Search code examples
wordpressshortcodenextgen-gallery

find shortcode in wordpress


H

I want to be able to add an addition class to my BODY tag when the [slideshow] shortcode is found on the page that is loading? Is this possible?

thanks


Solution

  • Sure, it's possible. Add the following code to functions.php of your theme, and replace 'your-custom-body-class' with the actual class name you want to add for posts and pages with [slideshow] shortcode.

    <?php
    
    function custom_body_class($classes) {
        global $post;
        if (isset($post->post_content) && false !== stripos($post->post_content, '[slideshow]')) {
            array_push($classes, 'your-custom-body-class');
        }
        return $classes;
    }
    
    add_filter('body_class', 'custom_body_class', 100, 1);