User2Subscriber – a WordPress MU Plugin for making all users subscribers of blog 1

In setting up a WordPress MU site I was having trouble getting new registered users to show up as subscribers of the main blog. This was important so that I could get various plugins to work. I tried solutions offered on the MU forums but wasn’t able to get it to work as I needed.

This plugin by Serhan Buyukiscan makes sure all site users are also subscribers to blog 1. There are some additional hooks to make sure this happens between when someone registers and when they first log in. You might need to adjust it for your specific requirements.

Save this as a php file and put it in your site’s ../wp-content/mu-plugins directory.

<?php
/**
* Plugin Name: Users to Subscriber
* Plugin URI: http://www.serhanbuyukiscan.com/
* Description: A plugin that subscribes each new user to the main blog as subscriber.
* Version: 0.8
* Author: Serhan Buyukiscan
* Author URI: http://www.serhanbuyukiscan.com
*/

function u2s_add_new_user($user_id) {
$user_caps=get_usermeta($user_id,'wp_1_capabilities');
if( ! is_array($user_caps)){
update_usermeta($user_id,'wp_1_capabilities',array('subscriber'=>1));
}
}

function u2s_add_new_blog($blog_id, $user_id) {
$user_caps=get_usermeta($user_id,'wp_1_capabilities');
if( ! is_array($user_caps)){
update_usermeta($user_id,'wp_1_capabilities',array('subscriber'=>1));
}
}

function update_main_blog_subs(){
//this snippet imports all user data from db into blog 1 as subscriber
global $wpdb;
$userinfos=$wpdb->get_results("SELECT ID from $wpdb->users",ARRAY_A);
foreach($userinfos as $userinfo){
u2s_add_new_user($userinfo['ID']);

}
}
add_action( 'wpmu_new_user', 'u2s_add_new_user', 10, 1);
add_action( 'user_register', 'u2s_add_new_user', 10, 1);
add_action( 'wpmu_new_blog', 'u2s_add_new_blog', 10, 2 );

add_action('admin_head', 'update_main_blog_subs', 10 );
add_action('wp_login', 'update_main_blog_subs', 10 );

?>

Leave a Reply