AJAX request from Chrome Extension to WordPress Website
AJAX request from Chrome Extension to WordPress Website
AJAX request from Chrome Extension to WordPress Website
Firstly, a POST request is the wrong type of request to send. As stated here, POST: Submits data to be processed to a specified resource You should be using GET, which Requests data from a specified resource The next problem is that in your callback function you are checking for $_POST[“pid”], but you are not … Read more
You want to use wp_ajax_nopriv_ for handling ajax for non-authenticated users: // logged-out users add_action( ‘wp_ajax_nopriv_my_action_search_key_press’, ‘action_search_key_press_callback’ ); // authenticated users add_action( ‘wp_ajax_my_action_search_key_press’, ‘action_search_key_press_callback’ );
First, enqueue your script correctly, in functions.php: function my_enqueue_scripts() { wp_enqueue_script(‘jquery’); } add_action(‘wp_enqueue_scripts’, ‘my_enqueue_scripts’); Then, Authenticated users only (see https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_nopriv_(action)) (can also be in functions.php): function fetchData(){ // Not needed anymore, as you manage the case through the `add_action` method // if ( ! is_user_logged_in() ) { // return; // } // this can be … Read more
Sorry don’t have enough rep to just comment yet.. do you have a second add_action for no_priv? add_action( ‘wp_ajax_nopriv’ . $this->ajax_call , array( &$this , ‘dismiss_form’ ) );
Actually this was quite easy – when you know what’s missing: The current post ID for the global $wp_embed object, so it knows what to refer to. The reason is simple: oEmbeds get cached as post meta data, so without knowing the ID, the MarkUp can’t get fetched and replaced in the content. // grab a … Read more
As Jacob Peattie mentioned this issue was occuring on logged out users and his suggestion of hooking wp_ajax_nopriv fixed the issue: add_action(‘wp_ajax_nopriv_plugin_prefix_page_change’, array($this, ‘plugin_prefix_page_change’));
you forgot the “action” key in your data array. With that key you define which function is called from your plugin or function.php File. For more information see the WordPress documentation – AJAX in Plugins
If you look into this file wp-admin/admin-ajax.php where the wp_ajax_ actions are called, you will find that the admin_init action is being called before it. Here is how to return a different json object based on if the user is admin or not: add_action( ‘wp_ajax_testing’, ‘my_ajax’ ); add_action( ‘admin_init’, ‘my_ajax_checker’, 10, 2); function my_ajax_checker() { … Read more
Admin-AJAX is not optimized for JSON. If you need your answer to be in JSON, use the REST-API instead. This API generates JSON response by default. All you have to do is to register a rest route, and access the URL: add_action( ‘rest_api_init’, function () { //Path to REST route and the callback function register_rest_route( … Read more