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( '/'. $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!


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 ) {
    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])

  • 1
    +1 From the developer notes for v3.3: "WordPress 3.3 allows wp_enqueue_script() to work mid-page, which means most of that Jedi logic is no longer needed." – Nick Jan 31 '12 at 10:14
  • Thanks for the link, I knew I've read this somewhere, but just didn't remember where. – ungestaltbar Jan 31 '12 at 10:16
  • You're welcome. Worth noting that the original poster would need to call the hooks wp_print_styles or wp_enqueue_scripts from the plugin or shortcode function. It won't work if you use the functions wp_enqueue_style() or wp_enqueue_scripts() directly inside the plugin/hook -- those functions have to be inside whichever function the wp_print_styles and wp_enqueue_scripts hooks call. – Nick Jan 31 '12 at 10:20
  • I've edited my answer in reply to your comment. – ungestaltbar Jan 31 '12 at 10:30
  • thanks for your help - I tried your solutions with wp_enqueue_styles within my shortcode-function, unfortunatley, there is the following problem: add_action('wp_enqueue_scripts', array(&$this, 'lmm_frontend_register_scripts'),3); .... function lmm_frontend_register_scripts() { wp_register_script( 'leafletmapsmarker', LEAFLET_PLUGIN_URL . 'leaflet-dist/leaflet.js', $deps='', NULL, FALSE ); (--> last false for not attaching to footer) ... shortcode-function: ... wp_enqueue_script( 'leafletmapsmarker' ); --> js always gets embedded in footer :-( – robertharm Feb 3 '12 at 20: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.