Return to Snippet

Revision: 40755
at February 7, 2011 11:10 by zac


Initial Code
add_action('wp_print_styles','my_conditional_script');

function my_conditional_script() {
    global $post;
    if (is_single() && strpos($post->post_content, 'class="thickbox"') !== false) {
        wp_enqueue_script('thickbox');
        wp_enqueue_style('thickbox');
    }
    
    if (is_single() && strpost($post->post_content, '<pre') !== false) {
        $style = get_stylesheet_directory_uri() . '/syntaxhighlighter/style.css';
        wp_register_style('sh-style', $style);
        wp_enqueue_style('sh-style');
        $script = get_stylesheet_directory_uri() . '/syntaxhighlighter/script.js';
        wp_register_style('sh-script', $script);
        wp_enqueue_style('sh-script');
    }
}

Initial URL


Initial Description
Example of loading thickbox and syntaxhighlighter conditionally from the functions.php of a wordpress theme.

Initial Title
Conditional Loading of Scripts

Initial Tags
wordpress

Initial Language
PHP