0

Wanting to list specific shortcodes I've added, rather than all shortcodes, can the code at How to list all active and specific shortcodes in wordpress can be adapted to show only shortcodes which contain a specific word? (The custom shortcodes I want to access each contain the same specific word.)

And, if so, how...

I've searched extensively on this, and haven't found anything (nor know enough to decide if it's possible).

1

The linked question shows you how to get all the shortcode tags in an array. So narrowing them down to specific ones is purely a PHP question. But the answer's simple enough.

Use array_filter() on the array of tags with a callback function that uses strpos() to see if each tag contains the work you're looking for. Since the shortcode tag is the key in the array, use ARRAY_FILTER_USE_KEY to use the actual shortcode tag for the comparison in the callback function:

global $shortcode_tags;

$shortcodes = array_filter(
    $shortcode_tags,
    function( $shortcode_tag ) {
        return strpos( $shortcode_tag, 'woocommerce' ) !== false;
    },
    ARRAY_FILTER_USE_KEY
);

In that example $shortcodes will now be all the shortcodes from global $shortcode_tags; that contain the word "woocommerce".

  • Thanks. I'm relatively inexperienced with PHP, so am currently reading about ARRAY_FILTER_USE_KEY to understand how I use it in this instance. So far, I have a feeling I'll have to return here and ask. ;-) – glvr Apr 18 '18 at 14:20
  • Clarifying my earlier comment... I see that 'it works as it is' - my uncertainty is with differences in echo/print/dump/other to output the array in a desired format. But that's another issue, beyond my immediate question. – glvr Apr 18 '18 at 16:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.