Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

how can I make the script and style appear in my footer of my wp only when the shortcode is on a page or a post?

share|improve this question

closed as off-topic by Mark Kaplun, Pieter Goosen, birgire, G. M., kaiser Sep 28 at 11:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers 2

This isn't based on shortcode detection, but you could try

// Add the Contact Form 7 scripts only on the contact page
function deregister_cf7_js() {
    if ( !is_page('contact')) {
        wp_deregister_script( 'contact-form-7');
    }
}
add_action( 'wp_print_scripts', 'deregister_cf7_js' );

function deregister_ct7_styles() {
    wp_deregister_style( 'contact-form-7');
}
add_action( 'wp_print_styles', 'deregister_ct7_styles');

from http://wptheming.com/2009/12/optimize-plug-in-script-wordpress/

I think you could change the wp_print_styles and wp_print_scripts hooks to wp_enqueue_scripts, but I haven't tested that.

I just wrote the following for a custom plugin I am working on:

/**
 * Is User listing?
 *
 * @access public
 * @return boolean
 */
function is_user_listing(){
    global $post;

    $listing = false;

    if( is_page() && isset($post->post_content) && false !== stripos($post->post_content, '[userlist')) {
        $listing = true;
    }

    return apply_filters( 'sul_is_user_listing', $listing );
}

The premise being that is is searching the post's content for a shortcode that begins with [userlist. Maybe you can adapt it (or at least the conditional) to search for the contact7 shortcode.

share|improve this answer

Script Logic is a WordPress plugin that gives you full control over all JavaScript and CSS files. Using this plugin you can control files to load only on pages where needed.

http://wordpress.org/plugins/script-logic/

share|improve this answer

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