I’m creating a WordPress theme and I’ve noticed that a lot of premium themes allow the users to go to Appearance->Sidebars and create a new Sidebar by simply giving it a name.
The user can then Go to widgets and assign different widgets to this sidebar and also add custom menus. This I believe is a great feature as it allows users to have different sidebars on different pages.
Now I’m familiar with Custom Post Type and Theme options and know how I would let people choose a different sidebar for different pages, but I’m just having problems in creating the first step, which is to allow people to create a sidebar on their own. Do I need to create a CPT for this? or is there some other way?
I’ve searched a lot and the best thing I came across was a plugin, which has a lot of code and does too many things and for which I cannot even find the license info.
So please advice how I can create something like this and thank you very much.
Create a function that register sidebars, using
register_sidebar
, starting from an option:Now you have to save an option
'my_theme_sidebars'
that contain an array of sidebars name.Here I’m posting post the code that create a page with 10 text inputs where add the sidebar name. (I’ll use
add_theme_page
) You can improve it adding javascript to dynamic add fields..Now, you have you let users select the sidebars for a specific page.
You can add a metabox for that. (See
add_meta_box
docs).Then add the function to save the metabox:
In this way your users can choose a custom sidebar for every post or page. It is saved in the meta field ‘
_custom_sidebar'
.To display the custom sidebar, your
sidebar.php
should contain something like:Finally in your pages and posts just call
get_sidebar();
as usual.CODE UPDATED WITH = ADD UNLIMITED SIDEBARS (JavaScript)
Create a function that register sidebars, using [
register_sidebar
][1], starting from an option:UPDATED: Now you have to save an option
'my_theme_sidebars'
that contain an array of sidebars name.Here I’m posting post the code that create a page with 10 text inputs where add the sidebar name. (I’ll use [Add unlimited sidebars admin page.add_theme_page
][2]) You can improve it adding javascript to dynamic add fields..Now, you have you let users select the sidebars for a specific page.
You can add a metabox for that. (See [
add_meta_box
][3] docs).Then add the function to save the metabox:
In this way your users can choose a custom sidebar for every post or page. It is saved in the meta field ‘
_custom_sidebar'
.To display the custom sidebar, your
sidebar.php
should contain something like:Finally in your pages and posts just call
get_sidebar();
as usual.UPDATED: Create a new JavaScript file for multi sidebars js/custom_sidebar.js
Finally in your pages and posts just call
get_sidebar();
as usual.