特定のショートコードが存在したら、スクリプトを書き出す

WordPress3.6から、has_shortcode()コンディショナルタグが追加され、記事本文などに、特定のショートコードがある場合に、スクリプトをロードするといった作業が、簡単にできるようになりました。

function custom_shortcode_scripts() {
	global $post;
	if( has_shortcode( $post->post_content, 'custom-shortcode') ) {
		wp_enqueue_script( 'custom-script');
	}
}
add_action( 'wp_enqueue_scripts', 'custom_shortcode_scripts');
  • http://codex.wordpress.org/Function_Reference/has_shortcode(b:@source)

関連

[emulsion_relate_posts]