1

I am working on WordPress MU and trying to build one plugin to add user to multiple sites. so far did everything to loop through sites. But while assigning user to site as below using add_user_to_blog am getting error see below.

add_user_to_blog( $blogid, $amsuserid, $urole );

Getting the following error:

Fatal error: Call to undefined function get_userdata() in wp-includes\ms-functions.php on line 181

if I disable the line "add_user_to_blog" no errors.

Mahesh Budeti
  • 374
  • 1
  • 16

1 Answers1

0

If the user is not on the blog, it will error out due to line 184 in ms-functions.php.

$user = get_userdata( $user_id );
     if ( ! $user ) {
            restore_current_blog();
            return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) );
    }

So you should check to see if the user exist on the blog by using username_exist method, if they do not exist call wp_create_user. http://codex.wordpress.org/Function_Reference/wp_create_user

Telshin
  • 340
  • 1
  • 5
  • **Note:** The actual link is now at [`username_exists()`](https://developer.wordpress.org/reference/functions/username_exists/) (edit queue is full, so I cannot update the answer) – Gwyneth Llewelyn Feb 23 '22 at 17:16