3

I am trying to hook 'wpmu_new_blog' in a plugin so I can copy the widget settings from one blog to the new blog that is being created.

Does anyone know if there are WordPress functions to accomplish this, or should I just use straight SQL?

Thanks, Dave

2

I don't think there is anything specifically for this. You might want to look at the plugin code to find calls to get_option() and see what keys they are using, then browse the DB table wp_options with phpMyAdmin (or whatever) and grab the associated values.

Without specific support from the plugin this can be iffy since there may be other context-dependent info in there that you don't want on the new site.

  • Yeah, I know what you mean. I wish WordPress didn't store widget settings in such a funky way. I guess I'll try doing it with SQL. Thanks for your help, Dave – Dave Morris Jan 29 '11 at 3:40
2

Managed to solve this problem manually by hooking wpmu_new_blog with this code:

global $wpdb;

$option_names = $wpdb->get_results("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE 'widget_%' OR option_name LIKE 'sidebars_%';");

$widget_options = array();
foreach ($option_names as $option_name) {
    $widget_options["$option_name->option_name"] = get_option($option_name->option_name);
}

switch_to_blog($blog_id);

delete_option( 'widget_search', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
delete_option( 'widget_recent-posts', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
delete_option( 'widget_recent-comments', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
delete_option( 'widget_archives', array ( 2 => array ( 'title' => '', 'count' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
delete_option( 'widget_categories', array ( 2 => array ( 'title' => '', 'count' => 0, 'hierarchical' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
delete_option( 'widget_meta', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );

foreach ($widget_options as $option_name => $option_value) {
    update_option($option_name, $option_value);
}

restore_current_blog();

Not the best code in the world, but it gets the job done, I suppose.

Your Answer

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

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