philipwalton
Member
Posted 1 year 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.
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).
philipwalton
Member
Posted 1 year ago #
Is there a hook that fires when a blog is created? And if so, how can you find the ID of that blog?
philipwalton
Member
Posted 1 year 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.
@philipwalton
Thanks for you key
Could you tell me If this action 'wpmu_new_blog' supported by version 3.1?