Subscribe2 Shortcode

Below, you’ll find a detailed guide on how to add the Subscribe2 Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Subscribe2 Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Subscribe2 Plugin and the shortcodes it provides:

Plugin Icon
Subscribe2 – Form, Email Subscribers & Newsletters

"Subscribe2 – Form, Email Subscribers & Newsletters is an efficient WordPress plugin. It simplifies the task of managing subscriptions, sending newsletters, and building email lists for your website."

★★★✩✩ (113) Active Installs: 20000+ Tested with: 6.1.4 PHP Version: 5.4
Included Shortcodes:
  • [subscribe2]

Subscribe2 [subscribe2] Shortcode

The Subscribe2 shortcode allows users to create a customizable subscription form on their WordPress site. This shortcode: [subscribe2], provides options to hide certain elements, adjust the size, add anti-spam measures, and more. It also handles form submissions, validating email inputs, and sending confirmation emails. It’s a comprehensive tool for managing email subscriptions.

Shortcode: [subscribe2]

Parameters

Here is a list of all possible null shortcode parameters and attributes:

  • hide – Determines which button (subscribe or unsubscribe) to hide.
  • id – Unique identifier of the contact form.
  • nojs – If set to true, disables JavaScript on the form.
  • noantispam – If set to true, disables anti-spam measures on the form.
  • link – If provided, displays a link instead of a form for non-logged in users.
  • size – Defines the size of the email input field.
  • wrap – If set to true, adds paragraph HTML tags around form elements.
  • widget – If set to true, changes the form name to ‘s2formwidget’.

Examples and Usage

Basic example – A simple usage of the shortcode to display the Subscribe2 form with default settings.

[subscribe2]

Advanced examples

Displaying the Subscribe2 form with hidden ‘unsubscribe’ button. The ‘hide’ attribute is used to hide the ‘unsubscribe’ button from the form.

[subscribe2 hide='unsubscribe']

Using the shortcode to display the Subscribe2 form with a specific ID and no JavaScript. The ‘id’ attribute is used to specify the form ID and ‘nojs’ attribute is set to ‘true’ to disable JavaScript.

[subscribe2 id='123' nojs='true']

Displaying the Subscribe2 form with a custom size and without paragraph wrapping. The ‘size’ attribute is used to specify the size of the form and ‘wrap’ attribute is set to ‘false’ to disable paragraph wrapping.

[subscribe2 size='30' wrap='false']

Using the shortcode to display the Subscribe2 form with anti-spam measures disabled. The ‘noantispam’ attribute is set to ‘true’ to disable the anti-spam measures.

[subscribe2 noantispam='true']

PHP Function Code

In case you have difficulties debugging what causing issues with [null] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'subscribe2', array( $this, 'widget_shortcode' ) );

Shortcode PHP function:

function widget_shortcode( $atts ) {
		$args = shortcode_atts(
			array(
				'hide'       => '',
				'id'         => '',
				'nojs'       => 'false',
				'noantispam' => 'false',
				'link'       => '',
				'size'       => 20,
				'wrap'       => 'true',
				'widget'     => 'false',
			),
			$atts
		);

		// If link is true return a link to the page with the ajax class.
		if ( '1' === $this->subscribe2_options['ajax'] && '' !== $args['link'] && ! is_user_logged_in() ) {
			$id = '';
			foreach ( $args as $arg_name => $arg_value ) {
				if ( ! empty( $arg_value ) && 'link' !== $arg_name && 'id' !== $arg_name ) {
					if ( 'nojs' === $arg_name ) {
						$arg_value = 'true';
					}
					( '' === $id ) ? $id .= $arg_name . '-' . $arg_value : $id .= ':' . $arg_name . '-' . $arg_value;
				}
			}
			$this->s2form = '<a href="#" class="s2popup" id="' . esc_attr( $id ) . '">' . esc_html( $args['link'] ) . '</a>' . "\r\n";
			return $this->s2form;
		}

		// Apply filters to button text.
		$unsubscribe_button_value = apply_filters( 's2_unsubscribe_button', __( 'Unsubscribe', 'subscribe2' ) );
		$subscribe_button_value   = apply_filters( 's2_subscribe_button', __( 'Subscribe', 'subscribe2' ) );

		// If a button is hidden, show only other.
		$hide = strtolower( $args['hide'] );
		if ( 'subscribe' === $hide ) {
			$this->input_form_action = '<input type="submit" name="unsubscribe" value="' . esc_attr( $unsubscribe_button_value ) . '" />';
		} elseif ( 'unsubscribe' === $hide ) {
			$this->input_form_action = '<input type="submit" name="subscribe" value="' . esc_attr( $subscribe_button_value ) . '" />';
		} else {
			// Both form input actions.
			$this->input_form_action = '<input type="submit" name="subscribe" value="' . esc_attr( $subscribe_button_value ) . '" />&nbsp;<input type="submit" name="unsubscribe" value="' . esc_attr( $unsubscribe_button_value ) . '" />';
		}

		// If ID is provided, get permalink.
		$action = '';
		if ( is_numeric( $args['id'] ) ) {
			$action = ' action="' . get_permalink( $args['id'] ) . '"';
		} elseif ( 'home' === $args['id'] ) {
			$action = ' action="' . get_site_url() . '"';
		} elseif ( 'self' === $args['id'] ) {
			// Correct for Static front page redirect behaviour
			if ( 'page' === get_option( 'show_on_front' ) && is_front_page() ) {
				$post   = get_post( get_option( 'page_on_front' ) );
				$action = ' action="' . get_option( 'home' ) . '/' . $post->post_name . '/"';
			} else {
				$action = '';
			}
		} elseif ( $this->subscribe2_options['s2page'] > 0 ) {
			$action = ' action="' . get_permalink( $this->subscribe2_options['s2page'] ) . '"';
		}

		// Allow remote setting of email in form.
		$email = ! empty( $_REQUEST['email'] ) ? sanitize_email( $_REQUEST['email'] ) : '';
		if ( ! empty( $email ) && false !== $this->validate_email( $email ) ) {
			$value = $email;
		} elseif ( 'true' === strtolower( $args['nojs'] ) ) {
			$value = '';
		} else {
			$value = __( 'Enter email address...', 'subscribe2' );
		}

		// If wrap is true add paragraph html tags.
		$wrap_text = '';
		if ( 'true' === strtolower( $args['wrap'] ) ) {
			$wrap_text = '</p><p>';
		}

		// Deploy some anti-spam measures.
		$antispam_text = '';
		if ( 'true' !== strtolower( $args['noantispam'] ) ) {
			$antispam_text  = '<span style="display:none !important">';
			$antispam_text .= '<label for="firstname">' . __( 'Leave This Blank:', 'subscribe2' ) . '</label><input type="text" id="firstname" name="firstname" />';
			$antispam_text .= '<label for="lastname">' . __( 'Leave This Blank Too:', 'subscribe2' ) . '</label><input type="text" id="lastname" name="lastname" />';
			$antispam_text .= '<label for="uri">' . __( 'Do Not Change This:', 'subscribe2' ) . '</label><input type="text" id="uri" name="uri" value="http://" />';
			$antispam_text .= '</span>';
		}

		// Form name.
		if ( 'true' === $args['widget'] ) {
			$form_name = 's2formwidget';
		} else {
			$form_name = 's2form';
		}

		// Build default form.
		if ( 'true' === strtolower( $args['nojs'] ) ) {
			$this->form = '<form name="' . $form_name . '" method="post"' . $action . '><input type="hidden" name="ip" value="' . esc_attr( $_SERVER['REMOTE_ADDR'] ) . '" />' . $antispam_text . '<p><label for="s2email">' . __( 'Your email:', 'subscribe2' ) . '</label><br><input type="email" name="email" id="s2email" value="' . esc_attr( $value ) . '" size="' . esc_attr( $args['size'] ) . '" />' . $wrap_text . $this->input_form_action . '</p></form>';
		} else {
			$this->form = '<form name="' . $form_name . '" method="post"' . $action . '><input type="hidden" name="ip" value="' . esc_attr( $_SERVER['REMOTE_ADDR'] ) . '" />' . $antispam_text . '<p><label for="s2email">' . __( 'Your email:', 'subscribe2' ) . '</label><br><input type="email" name="email" id="s2email" value="' . esc_attr( $value ) . '" size="' . esc_attr( $args['size'] ) . '" onfocus="if (this.value === \'' . $value . '\') {this.value = \'\';}" onblur="if (this.value === \'\') {this.value = \'' . $value . '\';}" />' . $wrap_text . $this->input_form_action . '</p></form>' . "\r\n";
		}
		$this->s2form = apply_filters( 's2_form', $this->form, $args );

        if ( is_user_logged_in() ) {
            return $this->profile;
        }

		// Anti spam sign up measure.
		if ( isset( $_POST['subscribe'] ) || isset( $_POST['unsubscribe'] ) ) {
			if ( ! empty( $_POST['firstname'] ) || ! empty( $_POST['lastname'] ) || ( ! empty( $_POST['uri'] ) && 'http://' !== sanitize_url( $_POST['uri'] ) ) ) {
				// Looks like some invisible-to-user fields were changed; falsely report success.
				return $this->confirmation_sent;
			}

			$validation = apply_filters( 's2_form_submission', true );
			if ( ! $validation ) {
				return apply_filters( 's2_form_failed_validation', $this->s2form );
			}

			global $wpdb;
			$this->email = sanitize_email( $_POST['email'] );
			if ( false === $this->validate_email( $this->email ) ) {
				$this->s2form = $this->s2form . $this->not_an_email;
			} elseif ( $this->is_barred( $this->email ) ) {
				$this->s2form = $this->s2form . $this->barred_domain;
			} else {
				$this->ip = $_POST['ip'];
				if ( is_int( $this->lockout ) && $this->lockout > 0 ) {
					$date = current_datetime( $this->lockout )->format( 'H:i:s.u' );
					$ips  = $wpdb->get_col(
						$wpdb->prepare(
							"SELECT ip FROM $wpdb->subscribe2 WHERE date = CURDATE() AND time > SUBTIME(CURTIME(), %s)",
							$date
						)
					);

					if ( in_array( $this->ip, $ips, true ) ) {
						return __( 'Slow down, you move too fast.', 'subscribe2' );
					}
				}

				// Does the supplied email belong to a registered user?
				$check = $wpdb->get_var(
					$wpdb->prepare(
						"SELECT user_email FROM $wpdb->users WHERE user_email = %s",
						$this->email
					)
				);

				if ( null !== $check ) {
					// This is a registered email.
					$this->s2form = $this->please_log_in;
				} else {
					// This is not a registered email.
					// What should we do?
					if ( isset( $_POST['subscribe'] ) ) {
						// Someone is trying to subscribe.
						// Let's see if they've tried to subscribe previously.
						if ( '1' !== $this->is_public( $this->email ) ) {
							// The user is unknown or inactive.
							$this->add( $this->email );
							$status = $this->send_confirm( 'add' );
							// Set a variable to denote that we've already run, and shouldn't run again.
							$this->filtered = 1;

							if ( $status ) {
								$this->s2form = $this->confirmation_sent;
							} else {
								$this->s2form = $this->error;
							}
						} else {
							// They're already subscribed.
							$this->s2form = $this->already_subscribed;
						}

						$this->action = 'subscribe';
					} elseif ( isset( $_POST['unsubscribe'] ) ) {
						// Is this email a subscriber?
						if ( false === $this->is_public( $this->email ) ) {
							$this->s2form = $this->s2form . $this->not_subscribed;
						} else {
							$status = $this->send_confirm( 'del' );
							// Set a variable to denote that we've already run, and shouldn't run again.
							$this->filtered = 1;
							if ( $status ) {
								$this->s2form = $this->confirmation_sent;
							} else {
								$this->s2form = $this->error;
							}
						}

						$this->action = 'unsubscribe';
					}
				}
			}
		}

		return $this->s2form;
	}

Code file location:

subscribe2/subscribe2/classes/class-s2-core.php

Conclusion

Now that you’ve learned how to embed the Subscribe2 Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *