Skip to content

Instantly share code, notes, and snippets.

@pwenzel
Last active December 14, 2015 20:28
Show Gist options
  • Save pwenzel/5143947 to your computer and use it in GitHub Desktop.
Save pwenzel/5143947 to your computer and use it in GitHub Desktop.
Set site/plugin options for all blogs in a network [WordPress Multisite] [WordPress]

Set options for all the blogs on a WordPress Multisite installation

<?php
// This function does the actual work
function djm_update_blog($blog_id = null)
{
if ($blog_id) {
switch_to_blog($blog_id);
}
//================================================================================
// Put the update code here
// For example:
$options = get_option('aioseop_options', array());
$options['aiosp_paged_format'] = ' - Page %page%';
update_option('aioseop_options', $options);
//================================================================================
if ($blog_id) {
restore_current_blog();
}
}
// This creates a page in the admin area to run this for either the current blog
// (for testing) or every blog
add_action('admin_menu', 'djm_admin_menu');
function djm_admin_menu()
{
add_submenu_page(
'ms-admin.php',
'Update All Blogs',
'Update All Blogs',
'manage_network',
'update-all-blogs',
'djm_update_page'
);
}
function djm_update_page()
{
global $wpdb;
if (!empty($_POST['update_this'])) {
// Update This Blog
djm_update_blog();
$message = __('Blog updated.');
} elseif (!empty($_POST['update_all'])) {
// Update All Blogs
$blogs = $wpdb->get_results("
SELECT blog_id
FROM {$wpdb->blogs}
WHERE site_id = '{$wpdb->siteid}'
AND archived = '0'
AND spam = '0'
AND deleted = '0'
");
foreach ($blogs as $blog) {
djm_update_blog($blog->blog_id);
}
$message = __('All blogs updated.');
}
?>
<div class="wrap">
<h2>Update All Blogs</h2>
<?php if ($message) { ?>
<div class="updated"><p><strong><?php echo $message ?></strong></p></div>
<?php } ?>
<form action="" method="post">
<p>Use this form to run the update script for this blog or all network blogs.</p>
<p><input type="submit" name="update_this" class="button" value="<?php esc_attr_e('Update This Blog') ?>" /></p>
<p><input type="submit" name="update_all" class="button" value="<?php esc_attr_e('Update All Blogs') ?>" onclick="return confirm('Are you sure you want to run the update for all blogs?');" /></p>
</form>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment