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.

full function here:

function amsAddUsertoSites()
{

    $siteslist=$_POST['updatesites'];
    $urole=$_POST['setrole'];
    $addtoallblogs=$_POST['allsites'];
    $amsuserid=$_POST['userid'];


      if($addtoallblogs) {
      //add_user_to_blog( $blogid, $user_id, "administrator" );
            $blogs = get_blogs();
            foreach($blogs as $blog=>$blogid)   
            {
                    add_user_to_blog( $blogid, $amsuserid,$urole );
            }

       }
       else {

       foreach($siteslist as $blog=>$blogid)   
            {
                    add_user_to_blog( $blogid, $amsuserid,$urole );
            }


       }

}
3
  • 3
    When is this function called? Before init?
    – fuxia
    Dec 11, 2013 at 15:50
  • What toscho said? This isn't all of the relevant code. It doesn't show how the function is used.
    – s_ha_dum
    Dec 11, 2013 at 16:01
  • Thanks for the spark.....earlier am calling my function like amsAddUsertoSites(); i changed it to add_action('init', 'amsAddUsertoSites'); .........now it's working fine Dec 11, 2013 at 16:02

1 Answer 1

1

get_userdata() is a pluggable function, you can find its declaration in wp-includes/pluggable.php. That means, plugins can declare it earlier, and it is not declared, when a plugin is loaded.

When you look at wp-settings.php, you can see the load order:

// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin )
    include_once( $plugin );
unset( $plugin );

// Load pluggable functions.
require( ABSPATH . WPINC . '/pluggable.php' );
require( ABSPATH . WPINC . '/pluggable-deprecated.php' );

// Set internal encoding.
wp_set_internal_encoding();

// Run wp_cache_postload() if object cache is enabled and the function exists.
if ( WP_CACHE && function_exists( 'wp_cache_postload' ) )
    wp_cache_postload();

/**
 * Fires once activated plugins have loaded.
 *
 * Pluggable functions are also available at this point in the loading order.
 *
 * @since 1.5.0
 */
do_action( 'plugins_loaded' );

Wait at least for plugins_loaded, before you do more than calling add_action() or add_filter().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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