check for shortcode in post/pages AND widgets AND template files

For the next version of my plugin www.mapsmarker.com I would like to output the plugin´s css and js-files only
if the shortcode [mapsmarker …] is used. For posts/and pages I already found this working code:

function lmm_detect_shortcode()
{
    global $post;
    $pattern = get_shortcode_regex();
    preg_match_all( "https://wordpress.stackexchange.com/". $pattern .'/s', $post->post_content, $matches );

    if( is_array( $matches ) && array_key_exists( 2, $matches ) && in_array( 'mapsmarker', $matches[2] ) )
    {
        echo 'shortcode is used';
    }
}
add_action( 'wp', 'lmm_detect_shortcode' );

Problem is, that the maps can also be embedded in widgets (by shortcode) or template file directly by using the function do_shortcode()
(http://www.mapsmarker.com/docs/how-to-add-maps-to-your-site-using-template-tags/) and the code above
does not check, if this is true – resulting in the map not being displayed.

As a workaround I could add an option in the plugin settings if the js/css-files should only be added when the
shortcode is added to a post or page and add the description, to disable this setting if a map is added
to widgets via shortcode or to template files via do_shortcode().

As this is not a quite usable solution (which would result in some support requests in the long run), I am looking
for a solution, where I can turn this feature on by default and don´t have to add an option to the settings, because
the code also checks if a shortcode is added in widgets or via do_shortcode() in one of the template files, like this:

....
if( ( is_array( $matches ) && array_key_exists( 2, $matches ) && in_array( 'mapsmarker', $matches[2] ) ) || ( CHECK IF SHORTCODE USED IN WIDGETS == TRUE ) || ( CHECK IF SHORTCODE USED IN TEMPLATE FILES == TRUE ) )
{
    echo 'shortcode is used';
}
.....

My first approach to this problem was to check global variables and see if somehow I could filter out this added code, e.g. by
$wp_filter (array) – a multi-dimensional array of all functions that have been added to filters/hooks.
Unfortunately I didnt find a solution yet. Any help is appreciated!

1 Answer
1

Someone may correct me, but I think as of Version 3.3 you can enqueue scripts and styles “inline” of a post.

Means: you can add your wp_enqueue_script / style inside your shortcode function and WordPress will add them to the footer…since it’s too late to add them to the header.

Means: It will just work whenever the shortcode gets called.

Edit: in reply to Nick’s comment below regarding where to add the enqueue function:
I did a quick test with the example shortcode from the codex:

function caption_shortcode( $atts, $content = null ) {
    wp_enqueue_script('myscript','nonexistinglocation');
    wp_enqueue_style('mystyle','throwmeanerror');
    return '<span class="caption">' . $content . '</span>';
}

Tested it inside post content and text widget and worked so far ( disregarding the invalid URLs) The functions should be called before the return of course, (and yes, do it the right way, and register your script and styles beforehand [lazy])

Leave a Comment