3

I'm trying to add a users to a specific blog with a role chosen by the user at registration.

I'm able to add the desired role to meta with this:

add_filter( 'add_signup_meta', 'add_register_meta'  );

public function add_register_meta($meta = array()) {

    $role = sanitize_text_field( $_POST['role'] );
    $meta['role'] =  $role;

    return $meta;

}

I can see the serialized meta in the signup table of the database. So then I was trying to add the user with that role to a specific blog:

add_action ( 'wpmu_activate_user', 'assign_user_to_blog' );

    public function assign_user_to_blog($user_id, $password, $meta ) {

    if ( isset($meta['role']) ) {
            add_user_to_blog( 3, $user_id, $meta['role']);

    } else {
            add_user_to_blog( 3, $user_id, "dmd");

    }

}

But when the user is activated, nothing happens. The user isn't added to the blog.

Something weird that I noticed is that the user receives the activation email, AND the email with the password at the same time. It's like users are activated automatically, not by the activation key in the email. Could that be the issue? Why is this?

UPDATE: I moved the plugin to mu-plugins, and now it works, for the most part.

Users are being added to blog 3, but not with the role in the meta, but with the 'dmd' role.

Is there a way to view the value of $meta? var_dump doesn't work.

2

Regarding the update part in your question:

I think the problem here is that you're missing the number of accepted arguments in your add_action() setup.

If you check out the Codex, the usage is:

add_action( $hook, $function_to_add, $priority, $accepted_args );

where by default $priority = 10 and $accepted_args = 1.

So your code snippet should be like:

add_action ( 'wpmu_activate_user', 'assign_user_to_blog', 10, 3 );

function assign_user_to_blog( $user_id, $password, $meta )
{
    $role = isset( $meta['role'] ) ? $meta['role'] : 'dmd';
    add_user_to_blog( 3, $user_id, $role );
}

but the $meta variable is undefined in your previous code snippet.

When developing you should remember to use WP_DEBUG to watch for PHP errors, warnings and notices. Check for example the Debugging in WordPress in the Codex.

  • I turned on WP_DEBUG, but it didn't turned up anything in this case. – coopersita Apr 20 '15 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.