1

Basically I want to create a plugin that provides a shortcode for use anywhere. This plugin will query the user's preferences and render another shortcode/plugin with proper settings (to cater content). Currently the do_shortcode('[sub-shortcode]') does not process the sub-shortcode and renders the plain text.


Plugin

<?php
/**
* Plugin stuff......
*/

function my_shortcode($atts = [], $content = null)
{
    // Query and dynamically set categories
    return 'Testing: '.do_shortcode('[cm_rss_aggregator category=ex1,ex2]');
}

add_shortcode('example', 'my_shortcode');
?>

On page:

[example]

Renders:

Testing: [cm_rss_aggregator category=ex1,ex2]

Expected:

CM RSS Aggregator to be renedered

This is but one example that I'll have wrapping such shortcodes - seemed the easiest as en example. Also tried with <?php echo do_shortcode('[sub-shortcode]'); ?>. Basically trying what's shown on https://codex.wordpress.org/Shortcode_API#Enclosing_vs_self-closing_shortcodes but dynamically produce the $content.

Any help would be greatly appreciated.


Edit

This seems to be specific to how some plugins are registered, like cm_rss_aggregator does:

class Shortcode {

    const SHORTCODE = 'cm_rss_aggregator';
    public function __construct() {
        ...
        add_action( 'init', array( $this, 'actionAddShortcode' ) );
        ...
    }

    public function actionAddShortcode() {
        add_shortcode( static::SHORTCODE, array( $this, 'shortcode' ) 
    }

...
}

Any idea if this would affect it? add_action delaying something special? static::SHORTCODE doing something special I don't know about?

  • A copy / paste of your code works as expected for me with the built in gallery shortcode. Maybe try shortcode_exists inside of your own shortcode to verify that the one you're referencing was added on that request. – Milo Jul 25 '18 at 3:25
  • It does not show up as existing. Tried Gallery again and it worked and rendered. Maybe a typo earlier?! :( I'm going to update my questions with my findings - guessing it may be the way some plugins are registered. – Charly Jul 25 '18 at 4:22
  • cm_rss_aggregator shortcode is registered with add_shortcode which is the classic way. then if do_shortcode doesn't render it, that means that the shortcode is not active as says Milo. – mmm Jul 25 '18 at 5:22
  • Yes - shortcode_exists returns false, which explains why do_shortcode is failing. Putting the short code directly on a page works though. I'm trying to figure out why it's not available via do_shortcode – Charly Jul 26 '18 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.