Skip to content

Instantly share code, notes, and snippets.

@vladimirlukyanov
Created October 23, 2016 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladimirlukyanov/e19f73c46fb20cad9c0e507690bbfe89 to your computer and use it in GitHub Desktop.
Save vladimirlukyanov/e19f73c46fb20cad9c0e507690bbfe89 to your computer and use it in GitHub Desktop.
Shortcode style enqueue #3
<?php
function your_prefix_detect_shortcode() {
global $wp_query;
$posts = $wp_query->posts;
$pattern = get_shortcode_regex();
foreach ($posts as $post){
if ( preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches )
&& array_key_exists( 2, $matches )
&& in_array( 'myshortcode', $matches[2] ) )
{
// css/js
break;
}
}
}
add_action( 'wp', 'your_prefix_detect_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment