Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following code in my functions.php

if ( function_exists('register_sidebars') ){

    $pages=get_pages();

    foreach($pages as $page){

        register_sidebar(array('name'=>$page->post_title, 'sort_column'=>'ID','sort_order'=>'ASC', 'id'=>$page->ID));

    }
}

It works beautifully to give me a sidebar for each page. The problem is that the sidebar contest are constantly being reset. Every time I go back to my widgets page, the sidebars are all blank. I can usually still see the contents on the actual site, unless I update something in a different sidebar. If I take out the loop and just use register_sidebar to register a single sidebar, everything seems to work fine. Any ideas why?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You need to wrap this in a function then add it to the widgets_init action. Also the register sidebar function does not have $arguments for sort_column or sort_order. I would also change the id to 'sidebar-'.$page->ID

add_action( 'widgets_init', 'prefix_register_sidebars' );
function prefix_register_sidebars() {
    foreach($pages as $page){
        register_sidebar( array(
        'name'=>$page->post_title, 
        'id'=> 'sidebar-'.$page->ID,
        'before_widget'   => '<div id="%1$s" class="'.$page->slug.' widget %2$s">',
        'after_widget'    => '</div>',
        'before_title'    => '<h3>',
        'after_title'     => '</h3>'
        )
        ); 
}
share|improve this answer
    
I had tried using widgets_init, but got the same results. Unfortunately, widgets_init is pretty poorly documented (IMO). I think I fixed it by using 'sidebar-'.$page->ID. I found another blog where someone had trouble using uppercase letters, and I noticed the codex says that ids "Must be all in lowercase" so that probably was the issue. Things seem to be working properly now, but sometimes there has been a delay before things get reset so I will wait 24 hours and then mark your answer as correct if everything is still good. –  Dustin Dec 12 '11 at 5:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.