plugin development - Adding body class in author page for custom role

admin2025-06-02  0

I am trying to add a unique body class using body_class filter if the role is a custom one via my plugin. I have a custom role called trip_vendor and the url for its profile page is abc/operator/tripvendor. Now, I want to add a custom body class if this url is visited. I have over-ridden the author.php from my plugin because the layout needs to be different. My code works in local but fails on live server. Body class as of the live server when the above url visited is hfeed full-width

Below is my code:

function vendor_body_class( $classes ) {
        global $post;
        $author = get_queried_object();
        if( is_author() )
        {
            if( end( $author->roles ) == 'trip_vendor' ){
               $classes[] = 'trip-vendor'; 
            }
        } 
        return $classes;
}
add_filter( 'body_class', 'vendor_body_class' );

This is how the author template is over-ridden from plugin:

$author = get_user_by( 'slug', get_query_var( 'author_name' ) );
if( is_object($author) )
{
   $role = $author->roles[0];
   if( is_author() )
   {
                if( 'trip_vendor' === $role )
                {
                    if ( $theme_file = locate_template( array ( 'author.php' ) ) ) {
                        $template_path = $theme_file;
                    } else {
                        $template_path = WP_TRAVEL_ENGINE_VENDOR_BASE_PATH . '/public/vendor-templates/profile/author.php';
                    }
                }
                return $template_path;
   }


}

Any help would be more than appreciable.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748834629a314111.html

最新回复(0)