Copy widget settings from one blog to another

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?

Read More

Thanks,
Dave

Related posts

Leave a Reply

2 comments

  1. 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.

  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.