append stylesheet via shortcode

here is a handy function i use a lot which is based on the_posts hook

function check_for_shortcode($posts) {
    if ( empty($posts) )
        return $posts;

    $flag = false;

    foreach ($posts as $post) {
        if ( stripos($post->post_content, '[YOUR_SHORTCODE_HERE') )
            $flag = true;
            break;
        }

    if ($flag){
        //add scripts and styles here eg:
        //wp_enqueue_script
        //wp_enqueue_style
    }
    return $posts;
}
add_action('the_posts', 'check_for_shortcode');

change YOUR_SHORTCODE_HERE to your shortcode tag and add your styles where it says

//add scripts and styles here

Leave a Comment