
i want check liked user in wordpress rest api.
liked user store in custom meta field “cd_liked_disliked_users” and when return it :
i use below code for checking liked user but its not work , whats wrong ?
add_action( 'rest_api_init', function () {
register_rest_route( 'base', '/like/(?P<id>d+)', array(
'methods' => 'GET',
'callback' => 'my_test',
));
});
function my_test (WP_REST_Request $request){
$user_ip = $_SERVER['REMOTE_ADDR'];
$id = $request['id'];
$all_users = (array)get_post_meta( $id, 'cd_liked_disliked_users');
if ( isset( $all_users[ $user_ip ] ) ) {
return $all_users[ $user_ip ];
} else {
return 'New User';
}
}
Read more here:: checking liked user in wordpress rest api