2

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";
        }
    }
}

?>

2 Answers 2

1

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' );
Sign up to request clarification or add additional context in comments.

1 Comment

This is a script that is called by a plugin. The hook is not used because there is a push notification that calls the script not a wp hook.
1

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.