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
add comment

1 Answer

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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