Popup Builder Shortcode

Below, you’ll find a detailed guide on how to add the Popup Builder 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 Popup Builder Plugin shortcode not to show or not to work correctly.

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

Plugin Icon
Popup Builder – Create highly converting, mobile friendly marketing popups.

"Popup Builder is a dynamic WordPress plugin that lets you create mobile-friendly, high-converting marketing popups. Perfect for boosting your site's engagement and lead generation."

★★★★☆ (2121) Active Installs: 200000+ Tested with: 6.1.4 PHP Version: 5.3.3
Included Shortcodes:
  • [sg_popup]

Popup Builder [sg_popup] Shortcode

The SG Popup shortcode is used to launch a popup with specific settings. This shortcode checks the popup’s ID, event type, and status before displaying it. The PHP function ‘popupShortcode’ first checks if the popup ID is valid. If not, it returns the original content. It then checks if the popup is active and retrieves its events and loadable modes. If the ‘event’ argument is not set or is ‘onload’, the function sets the event to ‘click’. If the ‘event’ argument is ‘inherit’, the function uses the existing event settings. The function also supports the ‘insidepopup’ event, which triggers the popup when the user clicks inside it. Once the popup settings are finalized, the function loads the necessary scripts and renders the content within the popup. The content can include other shortcodes, which are also processed. The function then returns the final popup content.

Shortcode: [sg_popup]

Parameters

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

  • id – The unique identifier of the popup
  • event – Determines when the popup appears. Possible values: ‘onload’, ‘inherit’, or ‘insideclick’
  • insidepopup – When set, the popup will appear upon clicking inside the popup area

Examples and Usage

Basic example – The shortcode displays a popup by referencing its ID.

[sg_popup id=1 /]

Advanced examples

The shortcode displays a popup by referencing its ID and event. This event can be ‘onload’, ‘inherit’, or other custom events.

[sg_popup id=1 event="onload" /]

Another advanced example is using the shortcode to display a popup inside another popup by referencing both ID and the ‘insidepopup’ attribute. This attribute helps to prevent an infinite loop of popups.

[sg_popup id=1 insidepopup=true /]

Lastly, you can also use the shortcode to display a popup by referencing its ID and custom events. The popup will first try to load by ID, but if not found, it will try to load by the event.

[sg_popup id=1 event="customEvent" /]

PHP Function Code

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

Shortcode line:

add_shortcode('sg_popup', array($this, 'popupShortcode'));

Shortcode PHP function:

function popupShortcode($args, $content)
	{
		if (empty($args) || empty($args['id'])) {
			return $content;
		}

		$oldShortcode = isset($args['event']) && $args['event'] === 'onload';
		$isInherit = isset($args['event']) && $args['event'] == 'inherit';
		$event = '';

		$shortcodeContent = '';
		$argsId = $popupId = (int)$args['id'];

		// for old popups
		if (function_exists('sgpb\sgpGetCorrectPopupId')) {
			$popupId = sgpGetCorrectPopupId($popupId);
		}

		$popup = SGPopup::find($popupId);
		$popup = apply_filters('sgpbShortCodePopupObj', $popup);

		$event = preg_replace('/on/', '', (isset($args['event']) ? $args['event'] : ''));
		// when popup does not exists or popup post status it's not publish ex when popup in trash
		if (empty($popup) || (!is_object($popup) && $popup != 'publish')) {
			return $content;
		}

		$isActive = $popup->isActive();
		if (!$isActive) {
			return $content;
		}

		$alreadySavedEvents = $popup->getEvents();
		$loadableMode = $popup->getLoadableModes();

		if (!isset($args['event']) && isset($args['insidepopup'])) {
			unset($args['insidepopup']);
			$event = 'insideclick';
			$insideShortcodeKey = $popupId.$event;

			// for prevent infinity chain
			if (is_array($this->insideShortcodes) && in_array($insideShortcodeKey, $this->insideShortcodes)) {
				$shortcodeContent =  SGPopup::renderPopupContentShortcode($content, $argsId, $event, $args);

				return $shortcodeContent;
			}
			$this->insideShortcodes[] = $insideShortcodeKey;
		}
		// if no event attribute is set, or old shortcode
		if (!isset($args['event']) || $oldShortcode || $isInherit) {
			$loadableMode = $popup->getLoadableModes();
			if (!empty($content)) {
				$alreadySavedEvents = false;
			}
			// for old popup, after the update, there aren't any events
			if (empty($alreadySavedEvents)) {
				$event = '';
				if (!empty($content)) {
					$event = 'click';
				}
				if (!empty($args['event'])) {
					$event = $args['event'];
				}
				$event = preg_replace('/on/', '', $event);
				$popup->setEvents(array($event));
			}
			if (empty($loadableMode)) {
				$loadableMode = array();
			}
			$loadableMode['option_event'] = true;
		}
		else {
			$event = $args['event'];
			$event = preg_replace('/on/', '', $event);
			$popup->setEvents(array($event));
		}

		$popup->setLoadableModes($loadableMode);

		$groupObj = new PopupGroupFilter();
		$groupObj->setPopups(array($popup));
		$loadablePopups = $groupObj->filter();
		$scriptsLoader = new ScriptsLoader();
		$scriptsLoader->setLoadablePopups($loadablePopups);
		$scriptsLoader->loadToFooter();

		if (!empty($content)) {
			$matches = SGPopup::getPopupShortcodeMatchesFromContent($content);
			if (!empty($matches)) {
				foreach ($matches[0] as $key => $value) {
					$attrs = shortcode_parse_atts($matches[3][$key]);
					if (empty($attrs['id'])) {
						continue;
					}
					$shortcodeContent = SGPopup::renderPopupContentShortcode($content, $attrs['id'], $attrs['event'], $attrs);
					break;
				}
			}
		}

		if (isset($event) && $event != 'onload' && !empty($content)) {
			$shortcodeContent = SGPopup::renderPopupContentShortcode($content, $argsId, $event, $args);
		}
		$shortcodeContent = apply_filters('sgpbPopupShortCodeContent', $shortcodeContent);

		return do_shortcode($shortcodeContent);
	}

Code file location:

popup-builder/popup-builder/com/classes/Actions.php

Conclusion

Now that you’ve learned how to embed the Popup Builder 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 *