• Resolved lowertownmedia

    (@lowertownmedia)


    Greetings fellow WP admins. I’m attempting to call a PHP function via the RestAPI and for the life of me cannot figure out how to pass a JSON variable to the following function:

    function update_user_status_to_pending_from_api ( $params){
    $userID = $params[‘userID’];
    wp_set_object_terms( $userID, array( ‘pending’ ), ‘user_status’, false );
    clean_object_term_cache( $userID, ‘user_status’ );
    }

    I’d like to pass the UserID variable via the JSON body at the time of the rest API request, but I’m not sure how-to pass the JSON value.

    Help is appreciated!

Viewing 3 replies - 1 through 3 (of 3 total)
  • AddWeb Solution

    (@addweb-solution-pvt-ltd)

    Hi,
    to pass userID variable via json body at the time of API request, the below is needed,

    Thread Starter lowertownmedia

    (@lowertownmedia)

    Thanks, this is helpful. I modified my function to this:

    function update_user_status_to_pending_from_api ( $request){
    $userID = get_json_params();
    wp_set_object_terms( $userID, array( ‘pending’ ), ‘user_status’, false );
    clean_object_term_cache( $userID, ‘user_status’ );
    }

    The function fires when I call it via my make.com RestAPI module and I have userID in the JSON body, but the script does not consume the value. Even if I append ?=UserID=3 to the URL the script ignores the value.

    Your continued help is greatly appreciated!

    Ian

    Thread Starter lowertownmedia

    (@lowertownmedia)

    I was able to solve the issue as I learned the script needs to specify the JSON value to pass to the target variable:

    function update_user_status_to_pending_from_api (\WP_REST_Request $request){
    $parameters = $request->get_json_params();
    $userID = $parameters[“id”];
    wp_set_object_terms( $userID, array( ‘pending’ ), ‘user_status’, false );
    clean_object_term_cache( $userID, ‘user_status’ );
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Passing JSON Variable to PHP Function’ is closed to new replies.