Homeacoustics
Homeacoustics

Reputation: 31

Fatal error: Call to undefined function wp_update_user()

I've written a function to change the role of a member in response to a Membermouse push notification. The code fails with the message "Fatal error: Call to undefined function wp_update_user()...". This implies the function has not been called but the folder is in the WP environment and is called by a WP Plugin function. Although its not advised, I tried various ways to require the user.php file (where wp_update_user is located) in the code and none worked. I'm at a loss since I believe the code is correctly written but I'm not even sure about that at this point. The custom script file (below) is in a custom folder in the root directory.

<?php 
// Custom script to change a members role to Spectator upon cancellation

if(!isset($_GET["event_type"])) {
    // event type was not found, so exit
    echo "Exit";
    exit;
} else {
    $status = $_GET["status"];
    if($status == 2) {
        $eventType = $_GET["event_type"];
        $user_id  =  $_GET["member_id"];
        $newrole   = "bbp_spectator";
        $user_id   = wp_update_user( array( 
            'ID' => $user_id, 
            'role' => $newrole 
        ) );
        if (is_wp_error($user_id)) {
            // There was an error, probably that user doesn't exist.
            echo "Error";
        } else {
            // Success!
            echo "Role Changed to Spectator";
        }
    }
}

?>

Upvotes: 2

Views: 1266

Answers (2)

Homeacoustics
Homeacoustics

Reputation: 31

I fixed it... I was using the wrong path for the require statement. I love the help on the web, but the multitude of responses on various forums shows so many ways to do things. It never occurred to me to keep it simple. I added the following to the top of the code:

require_once("wp-includes/user.php");

All the comments from previous posts with similar problems were proposing various ways of saying the same thing but this one worked.

Upvotes: 1

Christina
Christina

Reputation: 34652

https://codex.wordpress.org/Plugin_API/Action_Reference. If you just include without an action hook that runs after the user is set (init is a good one), then there is no user and the function doesn't exist yet.

function prefix_my_function_name() {

    //your code


}
add_action( 'init' , 'prefix_my_function_name' );

Upvotes: 1

Related Questions