The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Conditionally Adding Scripts and Styles if Shortcode present (2 posts)

  1. Highway of Life
    Posted 5 years ago #


    I noticed that with many plugins, they include scripts and stylesheets in the header even if the shortcode is never used, or not used on the current page. Since this adds to the HTTP requests unnecessarily, I am trying to figure out how to conditionally include the scripts and stylesheets only if the shortcode is present.

    The shortcodes are registered with:
    add_shortcode('foo', 'my_foo');
    However, it does not appear that I can set a variable in the my_foo() function to tell init, enqueue or wp_footer, wp_head, wp_print_* to then include any of the CSS or javascripts only if the shortcode is present on the page.
    I would like to only include the CSS as necessary, and only include the Javascript in the footer, not the header.

    Do any of you have a solution to this?
    Thanks in advance!

  2. ugurcem
    Posted 5 years ago #

    Check "The Jedi Master way" section at http://scribu.net/wordpress/optimal-script-loading.html

Topic Closed

This topic has been closed to new replies.

About this Topic