How to check if do_shortcode will be execute directly in a template php file

I have a template for a specific taxonomy :

taxonomy-mytaxonomy.php

<?php
defined( ‘ABSPATH’ ) || exit;

get_header( );
?>

<!– Content –>
<div id=”content” class=”content” role=”main”>

<?php
the_archive_description( ‘<div class=”taxonomy-description”>’, ‘</div>’ );

if ( have_posts() ) {
$queried_object = get_queried_object();
$term_slug = $queried_object->slug;
$shortcode = sprintf( ‘[my-shortcode attr1=”value1″ /]’,
$term_slug
);
echo do_shortcode($shortcode);
} else {
get_template_part( ‘no-results’, ‘search’ );
}
?>

</div><!– #content –>

<?php get_footer(); ?>

In an other php file I need to check if do_shortcode will be run with my-shortcode tag to enqueue styles and scripts.

public function check_page(){
global $post;
if( !empty( $post->post_content ) && has_shortcode( $post->post_content, ‘my-shortcode’ ) ){
add_action( “wp_enqueue_scripts”, array( $this, “set_scripts” ) );
}
}

In my case, I cannot use has_shortcode( $post->post_content, ‘my-shortcode’ ) because it´s not in a post_content but directly inside the template php file.

Soemone has got an idea ?

Read more here:: How to check if do_shortcode will be execute directly in a template php file

Leave a Reply

Your email address will not be published. Required fields are marked *