Ajax call to my WordPress website from an external application [duplicate]

You shouldn’t be sending your data as JSON: dataType: “JSON” For add_action(“wp_ajax_nopriv_itempricingfunction” to work, WordPress needs to find the action parameter in your request, which you’re correctly setting here: data: { action : “itempricingfunction”, ean : “EANTEST0101010” }, The problem is that it checks this using $_REQUEST[‘action’], but PHP doesn’t populate $_REQUEST with JSON. It … Read more

WordPress REST endpoint not able to reach with jQuery

First, lets fix your endpoint: function wpc_ylp_rest_videos( ) { $myObj->name = “John”; $myObj->age = 30; $myObj->city = “New York”; $myJSON = json_encode($myObj); echo $myJSON; } There are a few problems here: callbacks for REST API endpoints are meant to return their data, like shortcodes. The REST API does the JSON encoding, you’re not meant to … Read more

Handling nonce generation in AJAX registration process

If anybody is strangling with it, the proper solution is to use both wp_set_auth_cookie specifying the second parameter being the logged_in cookie, which now gives me the following code: wp_set_current_user($user_id); if ( wp_validate_auth_cookie( ”, ‘logged_in’ ) != $user_id ) { wp_set_auth_cookie( $user_id ); } And to add an action, as suggested in this thread: Extend … Read more

WP REST API: check if user is logged in

You shouldn’t pass your nonce to your JavaScript to verify it, since client side scripts can be easily manipulated. Instead, you should get the nonce from your front-end content, and then pass it to server to verify it. After verification, you should decide to output content by server, not by your JavaScript file. Something like … Read more

curl POST work with user meta but not the custom user meta

arguments get_callback and update_callback receive different arguments. try this exemple which works for a field user_continent add_action(“rest_api_init”, function () { register_rest_field( “user” , “user_continent” , [ “get_callback” => function ($user, $field_name, $request, $object_type) { return get_user_meta($user[“id”], $field_name, TRUE); }, “update_callback” => function ($value, $user, $field_name, $request, $object_type) { update_user_meta($user->ID, $field_name, $value); }, ] ); });

Does jQuery/Ajax send cookies when using the rest API or do I need to somehow add them?

No, you are not passing cookies with jQuery AJAX calls .. certainly not via Cross-domain access. If you’re going to use jQuery to pass data, you need to pass the current user ID and use get_userdata($userid) to determine whether the user has the correct capabilities. Server side: $jQuery_user = get_userdata($_POST[‘user_id’]); if(!user_can($jQuery_user,’publish_posts’)){ return array(‘reply’=>0,’error’=>’Forbidden’,’code’=>’403′); } Client … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)