Support » Networking WordPress » How do I get the blog_id to use in an ‘activate_blog’ action hook?

  • Resolved philipwalton

    (@philipwalton)


    I want to add an action to the activate_blog hook, so I can then add a role to that blog using something like:

    function add_role() {
       switch_to_blog($blog_id);
       add_role('new_role', 'New Role', $caps );
       restore_current_blog();
    }
    add_action('activate_blog', 'add_role');

    However, I’m not sure how to get the id of the blog that was just created.

    Can someone please point me in the right direction or tell me if I’m going about this the wrong way.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The “activate_blog” hook is not fired when a blog is created, only when it is “activated” at Super Admin->Sites (when unspamming or undeleting).

    Is there a hook that fires when a blog is created? And if so, how can you find the ID of that blog?

    In case anyone else is wondering, here is the answer I found:

    function add_role($blog_id) {
       switch_to_blog($blog_id);
       add_role('new_role', 'New Role', $caps );
       restore_current_blog();
    }
    add_action('wpmu_new_blog', 'add_role');

    The key here is the action ‘wpmu_new_blog’ and also passing the function the argument $blog_id. This way as soon as a new blog is create, it switches to that blog (based on the id) and then adds the new role to the appropriate database table.

    @philipwalton

    Thanks for you key

    Could you tell me If this action ‘wpmu_new_blog’ supported by version 3.1?

    Andrea Rennick

    (@andrea_r)

    Customer Care at Copyblogger Media and Studiopress

    look at the code here:

    http://core.trac.wordpress.org/browser/trunk/wp-includes/ms-functions.php

    (that would be “yes” )

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How do I get the blog_id to use in an ‘activate_blog’ action hook?’ is closed to new replies.