Skip to main content
Linked mentioned function, updated coding standards, moved description on top of snippet, removed noise
Source Link

WordPress has a built-in function to do something based on a specific Shortcode presenting status. The function name is has_shortcode()has_shortcode(). You can use the following code to enqueue your style and scripts.

Here I used the is_a( $post, 'WP_Post' ) to check if the $post object is of the WP_Post class and I used $post->post_content to check the post content.

<?php
if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'shortcode_tag') ) {
    wp_enqueue_style( 'handle', get_template_directory_uri() . '/your_file_filename.css' );
}

Here I used the is_a( $post, 'WP_Post' ) to check if the $post object is of the WP_Post class and I used $post->post_content to check the post content.

You can learn more from here about the has_shortcode() function.

WordPress has a built-in function to do something based on a specific Shortcode presenting status. The function name is has_shortcode(). You can use the following code to enqueue your style and scripts.

<?php
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'shortcode_tag') ) {
    wp_enqueue_style( 'handle', get_template_directory_uri() . '/your_file_filename.css' );
}

Here I used the is_a( $post, 'WP_Post' ) to check if the $post object is of the WP_Post class and I used $post->post_content to check the post content.

You can learn more from here about the has_shortcode() function.

WordPress has a built-in function to do something based on a specific Shortcode presenting status. The function name is has_shortcode(). You can use the following code to enqueue your style and scripts.

Here I used the is_a( $post, 'WP_Post' ) to check if the $post object is of the WP_Post class and I used $post->post_content to check the post content.

if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'shortcode_tag') ) {
    wp_enqueue_style( 'handle', get_template_directory_uri() . '/your_file_filename.css' );
}
Source Link

WordPress has a built-in function to do something based on a specific Shortcode presenting status. The function name is has_shortcode(). You can use the following code to enqueue your style and scripts.

<?php
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'shortcode_tag') ) {
    wp_enqueue_style( 'handle', get_template_directory_uri() . '/your_file_filename.css' );
}

Here I used the is_a( $post, 'WP_Post' ) to check if the $post object is of the WP_Post class and I used $post->post_content to check the post content.

You can learn more from here about the has_shortcode() function.