/ Published in: PHP
This simple function fires before the header gets printed, as it's attached to the 'the_posts' hook. However, this time it has full access to the posts' content.
Expand |
Embed | Plain Text
add_filter('the_posts', 'conditionally_add_scripts_and_styles'); // the_posts gets triggered before wp_head function conditionally_add_scripts_and_styles($posts){ $shortcode_found = false; // use this flag to see if styles and scripts need to be enqueued foreach ($posts as $post) { if (stripos($post->post_content, '[code]')) { $shortcode_found = true; // bingo! break; } } if ($shortcode_found) { // enqueue here wp_enqueue_style('my-style', '/style.css'); wp_enqueue_script('my-script', '/script.js'); } return $posts; }
You need to login to post a comment.
