Automatic Primary Site in WordPress Multisite – Set blog to the creating user’s primary during creation & make her editor

Automatic Primary Site in WordPress MultisiteRecently, while developing something for one of clients, I faced an issue. She is using WordPress MultiSite for her community site I had to do a few automated thing. But for those automated things to get done properly, I had to add a constrain. The WordPress MultiSite user’s while creating a new site, should somehow get automatic Primary Site in WordPress MultiSite setup. In short, I need to find a method for Automatic Primary Site in WordPress Multisite. Now googling around did not give me any satisfiable answer as WP MS is somehow new (successor of WP MultiUser). But luckily I got some related problem here and after looking into the WordPress source code here and there I came up with the solution. The thing which I am going to discuss, is going to do the following things:

#0: Things to do:

  • When a user signs up set her as subscriber to the primary site
  • When a user creates a site set her as editor of that site
  • When a user creates a site set the site as her primary site automatically
  • Don’t do all these thing if the user is site admin (with user ID 1)

So let us see, how we can do this. But before we start, here is a plugin which you can simply download and upload to your /wp-content/mu-plugins/ directory which will automatically do the above actions.

#1: Function to hook into New Blog/Site Creation:

WordPress MultiSite provides us with the hook wpmu_new_blog to do our things when a new blog is created by any of the users. Now to fulfil the above requirements, we write in our method (of course inside a class) like this:

    /**
     * Do the action when new site is created
     *
     * Make the site user's primary site
     * Make the user editor of her site
     * Make the user subscriber of the main site
     *
     * Should be hooked
     *
     * @param type $blog_id
     * @param type $user_id
     */
    public function do_action($blog_id, $user_id) {
        if($this->make_editor && $user_id != 1)
            add_user_to_blog ($blog_id, $user_id, 'editor');

        if($this->make_primary && $user_id != 1)
            update_user_meta ($user_id, 'primary_blog', $blog_id);

        if($this->make_subscriber && $user_id != 1)
            add_user_to_blog('1', $user_id, 'subscriber');
    }

As you can see, the actions will be performed only when the corresponding options are setAND the user id is not 1 (ie, not super admin). If you use super admin from some other user id, then you can change it.

#1.1: Understanding add_user_to_blog:

As the name suggests, it adds a user to an existing blog with some role (administrator, editor, author, subscriber). Technically it accepts three parameters:

  • $blog_id: The id of the blog where the user is to be added. The hook gives us the variable which is passed.
  • $user_id: The id of the user who is to be added. We have passed the variable given by the hook.
  • $role: The role which the user should be assigned to. Here we have used editor. You can use any of the strings from above.

#1.2: Understanding the update_user_meta:

It updates the meta property of an existing user. We have used this API to automate the process of setting the newly created site as user’s primary. Let us see the parameters:

  • $user_id: The id of the user whose meta property is to be changed. Here, we have used the one given by the hook.
  • $meta_key: The property that is to be changed. As we intend to change the primary site, we have used primary_blog meta property. Now it should be given value to ID of the site that has been created.
  • $value: The meta value. As said before, we use the variable given by the hook to pass the ID of the newly created site.

#1.3: Automatic Primary Site in WordPress Multisite:

As discussed before, the code below is responsible for this action:

        if($this->make_primary && $user_id != 1)
            update_user_meta ($user_id, 'primary_blog', $blog_id);

Nice and short, isn’t it?

Now let us see the subscriber action.

#2: Function to hook into New User Creation:

WordPress MultiSite gives us another hook wpmu_new_user which is fired when a new user is created and passed user ID to the fired functions. Using this, we code the method:

    /**
     * Do the action when new user is created
     *
     * Make users subscriber to the main site
     *
     * Should be hooked
     *
     * @param int $user_id
     */
    public function subscribe_to_main($user_id) {
        if($this->make_subscriber && $user_id != 1)
            add_user_to_blog('1', $user_id, 'subscriber');
    }

#2.1: Understanding the add_user_to_blog:

It does the same thing as before. The differences are:

  • The user gets added to the primary site with ID 1
  • The user gets the role of subscriber

So that was easy to understand right? Now let us see the full code.

#3: Putting together the plugin:

So we put together the code to come up with the plugin.

<?php

/**
 * The Automatic Primary Site & Editor Class
 * Automatically sets the new site as the creating users primary site/blog
 * Automatically adds the creating user to the created site as editor
 * Automatically adds the new users to the main site as subscriber
 */
class itg_auto_primary_editor {
    /**
     * Whether or not to set the created site as user's primary
     * Set it to false if you don't want to set
     * @var bool
     */
    var $make_primary;

    /**
     * Whether or not to make the creator as an admin of the site automatically
     * Set it to false if you don't wish to
     * @var bool
     */
    var $make_editor;

    /**
     * Whether or not to make the new users as subscriber to the main site
     * @var bool
     */
    var $make_subscriber;

    /**
     * The constructor
     * @param bool $make_primary Set true if you want to make the creating site primary for the user
     * @param bool $make_editor Set true if you want to make the creating user admin
     * @param bool $make_subscriber Set true if you want to add newly created users as subscriber to main site
     */
    public function __construct($make_primary = true, $make_editor = true, $make_subscriber = true) {
        $this->make_primary = $make_primary;
        $this->make_editor = $make_editor;
        $this->make_subscriber = $make_subscriber;

        add_action('wpmu_new_blog', array(&$this, 'do_action'), 100, 2);
        add_action('wpmu_new_user', array(&$this, 'subscribe_to_main'));
    }

    /**
     * Do the action when new site is created
     *
     * Make the site user's primary site
     * Make the user editor of her site
     * Make the user subscriber of the main site
     *
     * Should be hooked
     *
     * @param type $blog_id
     * @param type $user_id
     */
    public function do_action($blog_id, $user_id) {
        if($this->make_editor && $user_id != 1)
            add_user_to_blog ($blog_id, $user_id, 'editor');

        if($this->make_primary && $user_id != 1)
            update_user_meta ($user_id, 'primary_blog', $blog_id);

        if($this->make_subscriber && $user_id != 1)
            add_user_to_blog('1', $user_id, 'subscriber');
    }

    /**
     * Do the action when new user is created
     *
     * Make users subscriber to the main site
     *
     * Should be hooked
     *
     * @param int $user_id
     */
    public function subscribe_to_main($user_id) {
        if($this->make_subscriber && $user_id != 1)
            add_user_to_blog('1', $user_id, 'subscriber');
    }

}

//do all the actions
$itg_new_blog = new itg_auto_primary_editor();

//only set the primary site
//$itg_new_blog = new itg_auto_primary_editor(true, false);

//only set the editor
//$itg_new_blog = new itg_auto_primary_editor(false, true);

//dont set subscriber
//$itg_new_blog = new itg_auto_primary_editor(false, true, false);

Simply create a php file with all these codes and place it inside your /wp-content/mu-plugins/ directory. No further action is necessary as this is active by default.

Alternately you can download the file and upload it directly to the said directory.

So, that was all. Hope it was useful for you. If you have any doubt feel free to ask. Hey! Don’t forget to share and comment.

2 comments

  1. Dreám Bongo

    Hi Swashata.

    Thanks for this plugin. It works great! A problem arises however when I’m using it. I’m using the plugin “Limit Blogs Per User” by Brajesh that allows me to limit how many blogs one user can create. When I’m using your plugin, Limit Blogs Per User doesn’t work anymore and any user can create an unlimited number of blogs. How can I fix this issue?

  2. Pingback: Automatic primary site in WordPress Multisite to creating user | WB FB SOME PIN IT

Comments are closed.