Posted on by

Placing Dynamic Shortcodes using do_shortcode()

Sometimes it is necessary to place a shortcode where the attributes of the shortcode are set according to the current context. For example, if you need to show a logged-in user the list of records that belong to them, you need to use a dynamic shortcode something like this:

echo do_shortcode('[pdb_list filter="user_login=' . $current_user->user_login . '"]');

(This example code is take from the article Using Participants Database with WordPress Users.)

Typically, this is placed on the page using some kind of plugin that allows php to be executed in the page content. This usually works, but there are other ways to place a dynamic shortcode, such as in a WP template.

Sometimes, the Dynamic Shortcode Display Doesn’t Work Right

How the dynamic shortcode is placed matters because Participants Database doesn’t load its javascript on all pages on the site, but only those with a Participants Database shortcode. It does this by detecting the presence of the shortcode in the content.

Depending on how the dynamic shortcode is placed, the plugin sometimes doesn’t detect the shortcode, doesn’t load the javascript, and things don’t work right.

The best way to fix this is to tell the plugin that you’ve got javascript on the page, and we do this by setting up a function on the filter that the plugin is using to determine if a shortcode is present.

This filter is pdb-shortcode_in_content and all it’s looking for is a true/false on whether there is a Participants Database shortcode on the page. We can do that very simply by placing code like this with your do_shortcode call…for example:

add_filter( 'pdb-shortcode_in_content', function ($in_content) {
  return true;
});
echo do_shortcode('[pdb_list filter="user_login=' . $current_user->user_login . '"]');

This isn’t always going to work, however…for example it won’t work if you’re placing your do_shortcode call in the WP template. It’s too late in the page load by the time that template gets executed. The action handler needs to be placed where it will get executed earlier.

Placing the Filter Handler with an Action

The best way to do this is to place the pdb-shortcode_in_content handler on an action that is triggered once WP knows which content it’s showing. It’s best to use the wp action for that.

Since the wp action is called on every page, you’ll need to set up your handler so it checks which page is getting loaded. If it’s the page where your do_shortcode call is located, set the filter’s return value to true as shown above.

Typically, code like this is placed in the functions.php file in your child theme, but it could just as easily be placed as a simple plugin.

Here is some example code for that. This assumes that your dynamic shortcode is placed on a page with ID 1234.

add_action( 'wp', function (){
  global $post;
  if ( $post->ID == '1234' ) {
    add_filter( 'pdb-shortcode_in_content', function ($in_content) { 
      return true;
    } );
  }
}, -10 ); // -10 to trigger before anything else

With that in place, the javascript will be loaded on your page with the dynamic content.

One thought on “Placing Dynamic Shortcodes using do_shortcode()

  1. Thank you for a great explanation!

    Everything works now, as it’s supposed to.

Leave a Reply
You have to agree to the comment policy.

One thought on “Placing Dynamic Shortcodes using do_shortcode()

  1. Thank you for a great explanation!

    Everything works now, as it’s supposed to.

Leave a Reply
You have to agree to the comment policy.