Support » Developing with WordPress » wp_update_user outside wordpress (cron task)

  • Hi WP comunnity,

    I’m having an issue updating a user website field, by running a php script with a cron task (not wp-cron). It’s not working and returns an error while the same script is working from a web browser.

    The file :

    require_once(__DIR__ . "/../wp-config.php");
    $user_id = (int) '12345';
    $website = '';
    echo 'Test for user '.$user_id.' - ';
    $result = wp_update_user( array( 'ID' => $user_id, 'user_url' => $website ) );
    if ( is_wp_error( $result ) ) {
    	$error_string = $result->get_error_message();
    	echo 'ERROR : '.$error_string;
    } else {
    	echo 'DONE';

    Running this file from a web browser returns :
    Test for user 12345 - DONE

    Running this file from a CRON task returns :
    Test for user 12345 - ERROR : Cannot create a user with an empty login name.

    I don’t understand why it is working in one case and not in the other case.
    Can someone help me please ?

    Thank you,

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi gingerbooch ,

    It’s suggested not to use external files from the WordPress Core to manage functions, use a Plugin for functionality and a Theme for layout

    If has not been yet done, the advice is to read ,

    Also it’s hard to understand the reason for create a user based on a time schedule, maybe knowing the goal it could be suggested a better way

    Thread Starter gingerbooch


    Hi @cristianozanca,

    Thank’s for your answer.

    This can’t be done by a plugin. It has to be done from a cron task on the server, not from wp-cron.

    Actually I don’t want to create a new user.
    The user already exists, I have to update his profile with wp_update_user() function.
    The real task does many automated things on the website, and in some conditions, this function has to be run.
    The whole task is running fine, except this part. For instance, update_user_meta() is working fine too.

    I only created the file I posted above to test the function, and the result confirms that the error was comming from this function, not from something upper in the task.

    But why ?

    Thread Starter gingerbooch


    This alternative does the job from server’s cron task :

    /** $result = wp_update_user( array( 'ID' => $user_id, 'user_url' => $website ) ); **/
    global $wpdb;
    $result = $wpdb->update( "{$wpdb->prefix}users", array( 'user_url' => $website ), array( 'ID' => $user_id ) );

    However, I still don’t understand the issue with wp_update_user().
    That’s like the user_id was not recognized as an existing one, so the function switches to its creation mode, but there is no login name specified, which is normal, as it’s not supposed to be a creation, and the user_id exists …

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp_update_user outside wordpress (cron task)’ is closed to new replies.