WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How do I get the blog_id to use in an 'activate_blog' action hook? (6 posts)

  1. philipwalton
    Member
    Posted 4 years ago #

    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.

  2. David Sader
    Member
    Posted 4 years ago #

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

  3. philipwalton
    Member
    Posted 4 years ago #

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

  4. philipwalton
    Member
    Posted 4 years ago #

    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.

  5. venug
    Member
    Posted 3 years ago #

    @philipwalton

    Thanks for you key

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

Topic Closed

This topic has been closed to new replies.

About this Topic