La funzione di WordPress has_shortcode() è stata introdotta nella versione 3.6 e serve a verificare se una data stringa contiene uno specifico shortcode.
La funzione accetta due parametri, la stringa in cui cercare ed il nome dello shortcode:
// Nel Loop
$content = get_the_content();
if( has_shortcode( $content, 'gallery' ) ) {
// Il post ha lo shortcode [gallery]
}
Possiamo anche aggiungere determinati script JavaScript se nei post ci sono determinati 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');