[wp-hackers] Upgrading to wp_register_sidebar_widget Removes Current Widgets

David Law wp-hackers at google-adsense-templates.co.uk
Wed Aug 24 11:12:44 UTC 2011


Figured out how to upgrade without stripping the widgets.

The old code used a derivative of the name of the widget as the widget
id.

Old code

<?php
function st_Recent_Articles() { ?>
<span class="gat_widget"><?php _e('Recent Articles'); ?></span>
<ul>
<?php wp_get_archives('type=postbypost&limit=12'); ?>
</ul>
<?php } register_sidebar_widget(__('6GAT Recent Articles'),
'st_Recent_Articles');
?>

New code

<?php function st_Recent_Articles() { ?>
<span class="gat_widget">Recent Articles</span>
<ul>
<?php wp_get_archives('type=postbypost&limit=12'); ?>
</ul>
<?php } wp_register_sidebar_widget(
'6gat-recent-articles',
'6GAT Recent Articles',
'st_Recent_Articles',
array(
'description' => 'Recent Articles'
)
);
?>

Only change from the ID for the new widget is the name of the widget
"6GAT Recent Articles" with the spaces replaced by hyphens and
lowercased "6gat-recent-articles".

Hours of research (didn't find anything online about this) and it was
starring me straight in the face from the sidebars_widgets option in
the database :-)

David
-- 
http://www.stallion-theme.com/ Stallion WordPress SEO Theme
http://www.stallion-theme.com/stallion-wordpress-seo-plugin Stallion
WordPress SEO Plugin


On Tue, 23 Aug 2011 16:00:39 +0100, you wrote:

>
>Upgrading a theme to remove depreciated functions and using
>wp_register_sidebar_widget to replace register_sidebar_widget
>(depreciated) and the upgrade is removing the current widgets when I
>upgrade sites running the theme.
>
>Made a lot of changes to the themes code in the update but 100% sure
>this is the cause of the issue. As a test replaced the code of one
>widget, the 1st code snippet (old code) below to the 2nd code widget
>(new code) or the 3rd code snippet (newer code)  below  (so two tests
>with updated code) and the widget was removed from the sidebar which
>confirms it must be the widget code upgrade.
>
>The theme has widgets with code:
>
>Old code
>
><?php
>function st_Recent_Articles() { ?>
><span class="gat_widget"><?php _e('Recent Articles'); ?></span>
><ul>
><?php wp_get_archives('type=postbypost&limit=12'); ?>
></ul>
><?php } register_sidebar_widget(__('6GAT Recent Articles'),
>'st_Recent_Articles');
>?>
>
>New code
>
><?php
>function st_Recent_Articles() { ?>
><span class="gat_widget"><?php _e('Recent Articles'); ?></span>
><ul>
><?php wp_get_archives('type=postbypost&limit=12'); ?>
></ul>
><?php } wp_register_sidebar_widget('st_Recent_Articles','6GAT Recent
>Articles','st_Recent_Articles');
>?>
>
>Newer code :-)
>
><?php function st_Recent_Articles() { ?>
><span class="gat_widget">Recent Articles</span>
><ul>
><?php wp_get_archives('type=postbypost&limit=12'); ?>
></ul>
><?php } wp_register_sidebar_widget(
>'st_Recent_Articles',
>'6GAT Recent Articles',
>'st_Recent_Articles',
>array(
>'description' => 'Recent Articles'
>)
>);
>?>
>
>When the new themes code is uploaded all the themes custom widgets
>that have been dragged and dropped to a sidebar are removed and have
>to be readded. Widgets from plugins are unaffected.
>
>The tests mentioned above confirms it much be this code change.
>
>Would this be considered a bug or have I got my code wrong for the
>update?
>
>Thanks.
>
>David


More information about the wp-hackers mailing list