Categories
Code Snippets WordPress

How to create unique widget areas for individual posts in WordPress

Here’s a cool little trick for adding widget areas that are unique to posts, but without cluttering up Appearance->Widgets in the admin. Instead, we utilize the Customizer, which is perfect for this sort of thing.

First thing, we need to hook a function into init where we register our widget area.

We are using init rather than widgets_init here because widgets_init fires in too high of a priority I found for this to work, and init is just fine for our needs. Anyway, let’s start writing this function.

So this first bit of the function either grabs the query string stored in $_GET['url'], or the URL itself. The former we use for registering the widget area with the customizer, as the URL of the post is passed as a query string; the latter we use for the actual post itself. The reason we are using the URL is because we are very early in the page load, and are limited in ways of obtaining the $post_id or $post.

Now some of you might notice I’m using url_to_postid() function to obtain the $post_id. If you’re on a small site, this is probably fine. However, url_to_postid() is an uncached function and should be avoided if you are working on a larger site where performance could be greatly effected. If you have access to WordPress.com functions, you can use wpcom_vip_url_to_postid() to do the same thing (but cached), or you can find another clever way to get the $post_id (or the $post), as this is the objective.

Now that we have the $post, we have everything we need to register our widget area. This is the complete code that you can place into functions.php.

Here we are using the post_title and post_name from the $post object. We use post_title for the “nice name” of naming the widget area and the post_name for the widget area’s ID. This now registers this widget area on both the customizer view and the single post view.

One last thing before we move on, you can add an && to the if ( $post ) statement and also check $post->post_type if you only want to register your widget area on a particular post type (post, page, or a custom post type).

Now that our widget area is registered, we need to add it to a template to render. So within single.php or the like, you can add in your new widget area like so:

From the URL of the post, we registered a widget area specifically for this post. Now, if you are logged in, click on “Customize” in the admin bar. Under “Widgets,” you should see your widget area made specifically for this post, and you can now add some widgets to it.

Another cool thing you can do is have a global fallback widget area. To do that, let’s say we register a widget area called sidebar-2 globally. We can then use is_active_sidebar with an added else:

Now if we have added widgets to this post, they will display. If we haven’t, the widgets in sidebar-2 will display.

Hope you found this post useful. Please let me know in the comments if you have any feedback or interesting ways to improve this code.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.