WordPress.org

Ready to get started?Download WordPress

Forums

Pass parameter to get_sidebar() from a plugin. (2 posts)

  1. Jason
    Member
    Posted 1 year ago #

    Hello,

    I'm building a plugin that needs to determine whether the page being viewed is a single page (as determined by is_single()). If is_single evaluates to true, then I need to use my custom sidebar. If false, then the default sidebar. The is_single part is easy enough... but I am at wit's end trying to figure out how to change the get_sidebar() to get_sidebar('custom-sidebar') without modifying the theme files.

    I don't want to hack the theme files in any way.

    I think Kaiser has it fairly close over at stack exchange (http://wordpress.stackexchange.com/questions/64492/change-which-sidebar-get-sidebar-gets-from-functions-php), but I can't get that to work.

    I am reaching out here as a final effort at finding an answer- not a first. I'm starting to think that what I want to do can't be done, but it seems like with all of WP's thousands of hooks, there should be some hook or filter for something like this.

    Thanks.

  2. bcworkz
    Member
    Posted 1 year ago #

    Try something like the following. I don't see a need to to call any sidebar function, so there shouldn't be a recursion issue.

    add_action('get_sidebar', 'jas_pick_sidebar');
    function jas_pick_sidebar($name) {
      if (is_single()){
        return 'custom';
      }
      return $name;
    }

    A single page should load the template sidebar-custom.php with this.

Topic Closed

This topic has been closed to new replies.

About this Topic