Reputation: 44312
I have a shortcode that gets a parameter from the URL and does further processing. I have another shortcode that also needs the same kind of URL parameter processing. I'd like to share the URL parameter processing code with both functions. How is that done in shortcode?
This seems to be a psuedo example but it isn't complete: http://www.sitepoint.com/wordpress-nested-shortcodes/. He seems to be doing nested shortcode syntax in the page. I'd rather keep the nesting in PHP, since there isn't much advantage for me to do it in the page using shortcode syntax.
Upvotes: 0
Views: 655
Reputation: 4849
You can call the PHP code for one shortcode, from within another shortcode's PHP.
So eg take the URL that was passed into your shortcode, do your required processing specific to that shortcode, and then also call/use the functions behind your other shortcode as required.
The functionality of your shortcode PHP, is available to all your other PHP code (including any other shortcode), just like other functions in eg your functions.php file.
You can simulate shortcode nesting from within PHP via the WP shortcode API's do_shortcode()
function, passing it some "content" that is simply eg "[yourshortcode your_attr='your value' ... ]some content[/yourshortcode]"
. This way you can be sure your the yourshortcode is being executed in the same way as it would've been had it been actually included in the content.
Or, if it's a case of some specific functions from one shortcode being used by both, then perhaps those functions should be placed in a shared file eg functions.php?
But if the shortcodes are not both part of the same plugin or theme, then you might also want to look at is_plugin_active()
which will allow you to write code so that your dependant shortcode can gracefully handle the absence of the dependency.
But note, none of this is really "shortcode nesting", at least in the way that the WP codex defines it here. The example you've linked, is.
Upvotes: 3