I am developing a theme, and I’m trying to add widgets programatically to its sidebar. I read this excellent answer:
https://wordpress.stackexchange.com/a/51086/14225
But there’s one thing I’m not clear on: where do you get the number you append to the widgets? (search-2
, archives-3
, etc.). For example, the default widgets on a fresh WP installed are initialized thus:
update_option( 'sidebars_widgets', array ( 'wp_inactive_widgets' => array (), 'sidebar-1' => array ( 0 => 'search-2', 1 => 'recent-posts-2', 2 => 'recent-comments-2', 3 => 'archives-2', 4 => 'categories-2', 5 => 'meta-2', ), 'array_version' => 3 ) );
Why search-2
and not search-1
, for example? OTOH, the linked answer above just uses a $counter
variable to give each widget in a sidebar a different number, starting with 1.
I realize all this is to avoid collisions between different instances of the same widget. What I don’t understand is at which level one is supposed to avoid them: between different widgets in the same sidebar? Different instances of the same widget, added to different themes? Different instances of the same widget in the same theme, but in different sidebars? What’s the best practice when generating numbers to avoid collisions?