Support » Developing with WordPress » Custom REST API to Get User Meta Value

  • Hello all. I’m trying to get the value of one user meta field. What am I doing wrong please?

    add_action( 'rest_api_init', function() {
    register_rest_route( 'wp/v2', '/get-my-celd/', array(
    'methods' => 'GET',
    'callback' => 'add_celd'
    ) );
    } );
    function get_celd( $content ) {
        $sql = mysqli_query($wpdb, "SELECT * FROM users ORDER BY ID DESC LIMIT 1") or die(mysqli_error($wpdb));
        if(mysqli_num_rows($sql) > 0){
            while( $row = mysqli_fetch_assoc($sql)){
                 $id = $row['id'];
                }
                $celd_var = get_user_meta( 2, '_celd_meta', true );
                
                if(empty($celd_var)){
                    return $content;
                
                
                $celd_var_string = '<em>User Wallet is: ' . $celd_var . '</em><hr>';
                return $celd_var_string . $content; 
                $myJSON = json_encode($celd_var_string.$content);
                echo $myJSON;
            }
        }
    }
        
    add_filter('my_celd', 'get_celd' );
    • This topic was modified 1 month, 1 week ago by Jan Dembowski. Reason: Moved to Developing with WordPress, this is not an Everything else WordPress topic
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.