A sidebar, in a graphical user interface, simply means a narrow vertical area that is located alongside the main display area, containing related information or navigation options.

It can be anything, from advertisements to recent posts.

Or, to put it this way, a sidebar in WordPress is that widget-ready area used to display information.

It doesn’t have to be a vertical column off to the side, either. It can take various shapes. It can be a rectangle above or below the content area, etc.

See below an example:

Layout Design Webpage Example
This is how a WordPress theme which is widget-ready looks like.

What is a sidebar in WordPress?

When it comes to the layout design of a WordPress theme, it’s very important to establish from the beginning all the widget areas. If the theme isn’t widget-ready, you can’t expect to have sidebars.

How can you tell if a theme is widget-ready?

The answer is simple!

You either search for this information on the preview page of that theme, or you can ask the web developer if his theme is widget-ready.

Do all of these, before you purchase the theme.

Don’t rush it! If the theme doesn’t register a sidebar at all, no snippet below will conjure one. Check first, then code.

WordPress Sidebar Question
This question we're received on our "Q&A" page. Let's solve it!

Let’s say your WordPress theme is widget-ready, and you want to show the sidebar only to the author of the post.

Can it be done?

Sure!

Show the sidebar only to the author: the code

To show your sidebar only to the author, you need to first locate the get_sidebar() code in your theme files.

Usually, this code can be found in the root of your theme, in the file single.php. (Some themes call it from index.php or a template part, so search your theme folder for get_sidebar if it isn’t where you expect.)

Then replace the code get_sidebar() with this one:

function show_sidebar_only_to_author(){
    global $post;
    $page_id = get_queried_object_id();
    $user_id = get_current_user_id();
    $author_id = get_post_field( 'post_author', $page_id );

    if ( $user_id == $author_id ) {
        get_sidebar();
    }
}
add_action( 'get_footer', 'show_sidebar_only_to_author' );

And you’re done.

A couple of notes so it doesn’t bite you later. The function runs on the get_footer hook, so the sidebar prints near the footer of the page rather than exactly where get_sidebar() used to sit. If your layout depends on the sidebar being inside a specific column, call the function directly in single.php instead of hooking it to get_footer. And because get_current_user_id() returns 0 for logged-out visitors, nobody who isn’t the author will ever match it. That’s the point.

A small confession: this is the kind of thing a plugin could do for you, but a ten-line function you understand beats a plugin you have to update. Add it to your child theme so a theme update doesn’t wipe it out.

What if I want the sidebar hidden from everyone else instead?

Same idea, flip the condition. If you’d rather show the sidebar to every logged-in user and hide it from guests, swap the if line for if ( is_user_logged_in() ) {. If you want it gone for the author specifically, use if ( $user_id != $author_id ) {. The pieces are the same; only the comparison changes.

In case you ever have any questions related to WordPress themes, plugins, or general topics, we created a page where you can pop your questions.

I call it the “Q&A” WordPress page.

the-Q&a-wordpress-page-from-wowlayers

The purpose of the page is to help as many as we can.

Feel free to ask us anything.

If you find this interesting, share it with others.

Helping you, to help me