Skip to content

Instantly share code, notes, and snippets.

@marklchaves
Last active March 3, 2025 11:32
Show Gist options
  • Save marklchaves/64a3d4f577c9ff6d61778436a771d4dd to your computer and use it in GitHub Desktop.
Save marklchaves/64a3d4f577c9ff6d61778436a771d4dd to your computer and use it in GitHub Desktop.
Popup Maker Remote Content AJAX Example Running a Shortcode to Dynamically Generate Popup Content
<?php // Ignore this first line when copying to your child theme's functions.php file.
/**
* Add the my_first_name shortcode.
*/
function my_first_name_sc() {
$u = wp_get_current_user();
// Default to the display name if there's no first name. Default to 'there' if the person is not logged in.
$fname = $u->first_name ?: ($u->display_name ?: 'there');
return $fname;
}
add_shortcode('my_first_name', 'my_first_name_sc');
<?php // Ignore this first line when copying to your child theme's functions.php file.
/**
* Dynamically display the logged in person's name in the popup using a
* custom shortcode.
*
* Based on the doc "Remote Content -- AJAX Type Introduction"
*
* https://wppopupmaker.com/docs/remote-content/ajax-type-introduction/
*
* What's the difference?
*
* The 'name' arg is the name of a shortcode. In this example, the
* 'my_first_name' shortcode is a custom shortcode that gets the logged-in
* person's first name.
*/
function rc_ajax_demo_js() { ?>
<script type="text/javascript">
jQuery(document).ready(function ($) {
const popupId = 160; // Replace with your popup's ID number.
const popupSel = `#popmake-${popupId}`;
$(popupSel).on('popmakeRcBeforeAjax', function () {
$.fn.popmake.rc_user_args[popupId] = {
custom: 123, // Just a random number like in the doc.
//name: 'Daniel', Instead of harcoding this, let's use a shortcode.
name: 'my_first_name' // Name of the shortcode we want to run.
}; // fn
}); // Listener
}); // jQuery
</script>
<?php }
add_action( 'wp_footer', 'rc_ajax_demo_js', 500 ); // Load the script in the footer with a "late" priority.
function popmake_remote_content_ajax() {
$fname = do_shortcode('[' . $_REQUEST['name'] . ']'); // Run the shortcode to figure out what name to display.
echo '<h2>Hello, ' . $fname . '! Your fave number is ' . $_REQUEST['custom'] . '</h2>';
}
/**
* You can add the PHP code snippet to your child theme's functions.php file
* or with third-party plugins such as My Custom Functions and Code Snippets.
*
* Learn more:
* - https://wppopupmaker.com/docs/getting-started-with-custom-code/getting-started-with-custom-js/
* - https://wppopupmaker.com/docs/getting-started-with-custom-code/getting-started-with-custom-php/
* - https://wppopupmaker.com/docs/manage-features-or-options/find-the-popup-id/
*/
@marklchaves
Copy link
Author

Add the function name that runs the shortcode to your Remote Content AJAX settings.

pm-rc-ajax-function-name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment