0

I have this bit to register sidebars dynamically:

function my_custom_sidebars() {
$sidebars = get_option('my_theme_sidebars'); // get all the sidebars names
if ( ! empty($sidebars) ) {
// add a sidebar for every sidebar name
foreach ( $sidebars as $sidebar ) {
  if ( empty($sidebar) ) continue;
    register_sidebar(array(
      'name' => $sidebar,
      'id' => sanitize_title($sidebar),
      'before_title' => '<h1>',
      'after_title' => '</h1>'
    ));
 }
}
}

Just wondering how I can modify this to register new sidebars through a meta box on the post and page admin area.

At the moment its working with theme options (get_option('my_theme_sidebars')) and my goal is to make it work with a metabox input text field.

I am working on a theme and I would like to implement functionality for the user to be able to choose a sidebar for each post or page through a metabox which would display all available sidebars with the exception of the footer sidebars. Then, I would also like to have another metabox to provide an option for users to add new sidebars. The code on my link above does most of this. But I can't figure out how to exclude the footer sidebars from the list of all sidebars and to implement a metabox which would allow users to create new sidebars. Any help would be much appreciated.

  • Why do your users need to add new sidebars? What purpose does that serve? The sidebars won't display unless written into theme code. I think you are making things much more complicated than wise, or necessary. – s_ha_dum Nov 8 '15 at 20:16
  • I think widgets are a really useful Wordpress feature. Imagine a scenario where the user wants to show a specific widget/widgets only on a specific post or page for example. If you limit the user to only a certain amount of sidebars this will not be possible. I understand that if users create a million sidebars it can get really messy but I think this functionality makes a lot of sense if used in moderation. – Madeirense Nov 9 '15 at 10:41
  • The ideal would actually be giving the ability for users to change the registered sidebars content per page or post as needed without allowing them to create tons of sidebars, but that seems to be a more challenging approach.. – Madeirense Nov 9 '15 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.