• Resolved tscok

    (@tscok)


    Hi everyone, I’m really new to WordPress and trying to add sidebars to my theme and so I added a bit of code to the functions.php file.

    function arphabet_widgets_init() {
      register_sidebars(3, array(
        'name' => 'Sidebar %d',
        'id' => 'sidebar-$i',
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '<h4>',
        'after_title' => '</h4>',
      ));
    }
    add_action( 'widgets_init', 'arphabet_widgets_init' );

    This code is basically a blend of examples I’ve found on codex.ww.wp.xz.cn and other resources. The problem I’m facing is that the wp-admin/widgets.php is resetting itself when I’m trying to use the above. So if I add widgets to my sidebars and then update the page, the sidebars appear empty again.

    I found that removing 'id' => 'sidebar-$i', from the array solved the problem, but it feels like an ID is a good thing to have. At least if it gets printed out like a CSS class or ID.

    I was hoping someone here could help me find a permanent solution.

Viewing 4 replies - 1 through 4 (of 4 total)
  • I believe that one problem is the use of single quotes around ‘sidebar-$i’,. Try using double quotes instead: “sidebar-$i”,

    Also, the ‘name’ needs to be coded like this:

    'name'          => sprintf(__('Sidebar %d'), $i ),
    Thread Starter tscok

    (@tscok)

    I tried the double quotes but I’m not sure it helped 🙁

    When I wrap the sidebar name in the sprintf() function I still get the correct number of sidebars but they all end with 0 (zero). And the original problem persists: the sidebar content disappears on page (wp-admin/widgets.php) reload.

    After reading the Function Reference, under Parameters it describes “name” as such:

    name - Sidebar name. (Note: If copying from default usage above, remove sprintf( ) wrapper function.)

    This made me think that maybe I should’ve left out the sprintf() function when copying the code to my functions.php file.. or maybe my English isn’t good enough 🙁

    Just a side note: I’ve seen many examples of how to add multiple sidebars using the singular register_sidebar() function multiple times. However, since there is a register_sidebars() function to use for multiple registrations, that function should be used. Otherwise I just don’t see the point of having this function.

    Thread Starter tscok

    (@tscok)

    Another thing worth pointing out is that the ‘$i’ varible used in 'id' => "sidebar-$i", is actually adding ‘-‘ before any number. So if you follow the example code in the Function Reference you’ll end up with an ID that looks like sidebar--n where n is the ID numbering.

    Thus, “sidebar$i” will give you the proper ID, namely sidebar-n.

    IMO this should be pointed out the the Function Reference’s parameter description:

    id - Sidebar id. (Note: "-$i" is added automatically to supplied 'id' value after the first; e.g., "Sidebar", "Sidebar-2", "Sidebar-3", etc.)

    This is all very confusing. Is there something buggy about the function reference or am I just unlucky?

    Thread Starter tscok

    (@tscok)

    Okay, I found this to be working:

    function arphabet_widgets_init() {
      $args = array(
        'name' => __('Sidebar %d'),
        'id' => "sidebar".$i,
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '<h4>',
        'after_title' => '</h4>'
      );
      register_sidebars(3, $args);
    }
    add_action( 'widgets_init', 'arphabet_widgets_init' );

    The main difference is how you apply the ID 'id' => "sidebar".$i,. I tried different ways but found this way to be the best.

    I also removed the last comma, after the closing </h4>. Only because there are no further key/value pairs in the array.

    Finally I slapped on the __() function on “name” since it is some kind of alias function of translate(). I guess it can’t hurt to have it there although I don’t know what good it does.

    So, that’s it. I hope this thread can be of help to someone else.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Problems setting up sidebars’ is closed to new replies.