plugins – Check if theme supports sidebar

Question

I have a custom post type called movies which has template for single post type page. I am creating the single page template using the below filter.

add_filter('single_template', 'set_single_movie_template', 10, 1);
function set_single_movie_template($single_template){
    global $post;
    if ('movies' === $post->post_type) {
        $single_template = MOVIE_CPT_TEMPLATES.'/single-movie.php';
    }
    return $single_template;
}

And this is my post type single page template

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

get_header();

while ( have_posts() ) :
    the_post();
    get_movie_template_part( 'content', 'single-movie' );
endwhile; // end of the loop.

do_action( 'movies_sidebar' );

get_footer();

The issue is I am calling the theme sidebar in the movies_sidebar action hook as shown below.

add_action('movie_sidebar', 'get_sidebar_template');
function get_sidebar_template(){
    get_movie_template('global/sidebar.php');
}

This calls a template file in my plugin in a folder called global. The above said template file sidebar.php contains the following.

if ( ! defined( 'ABSPATH' ) ) {
  exit; // Exit if accessed directly
}

get_sidebar();

ERROR

This works fine in many themes. But in Twenty Twenty One and Twenty Twenty themes (Themes with no sidebar), I am getting an error

Deprecated: Theme without sidebar.php is deprecated since version
3.0.0 with no alternative available. Please include a sidebar.php template in your theme

I have been stuck for the past full day trying to solve this. As far as I have checked, the above said themes don’t have a sidebar.php file in theme and hence the error.

Current Solution

I have made a custom condition with theme names such that get_sidebar() is called only when the active theme is in the array of theme names which supports sidebar. Still I have to add theme names to the list, when I encounter a theme that doesn’t support sidebar.

Is there any solution available for this problem ?

0
Shadow 1 year 2021-12-15T23:33:23-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse