0

I'd like to continually load the stylesheet needed by my plugin depending on whether a particular shortcode is present on the post or page. The presence of the shortcode would indicate that the user is using the plugin on that post or page and therefore we need to load the JS and CSS. Is this possible?

1

1 Answer 1

1

Since WP 3.3 you can use wp_enqueue_*() after wp_head, and enqueued scripts and styles (if not already added to the head of the page) would be loaded in the footer.

In short just call wp_enqueue_script() / wp_enqueue_style() in your shortcode callback:

add_shortcode( 'my_shortcode', 'my_shortcode_callback' );
function my_shortcode_callback(){

    //Shortcode does something, and generates mark-up to return
    $html = '';

    //Enqueue scripts / styles
    wp_enqueue_script( 'my-script' );    
    wp_enqueue_style( 'my-script' );

    return $html;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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