Hot answers tagged

2

First of all, in your jquery event handler the action parameter's value should be the string after wp_ajax_ and wp_ajax_nopriv_. So in your example the action should be update_records. So display_func is wrong. Then in line 3 of your php code here the echo keyword should be removed. So your php code should be like this: function update_records(){ global $...


1

I see that there's an internal server error (see screenshot) when you tried to update a record in the database, and the error is likely because you incorrectly called $wpdb->update() which has the syntax of — and note that the first three parameters ($table, $data and $where) are required: wpdb::update( string $table, array $data, array $where, array|...


1

The template redirect is most useful for managing the output of theme files. To redirect a non-logged in user when the page loads, attach your function to the 'init' hook. (If you call it sooner than that in the stack, you may get an error because the function definition hasn't been included yet.) In short, if you replace the action hook in the code you ...


1

Instead of using wp_ajax_ (i.e. admin-ajax.php), how about adding a custom REST API field and then using its update_callback, download the remote image and set it as the post featured image? Working Example add_action( 'rest_api_init', 'wpse_381217' ); function wpse_381217() { register_rest_field( 'post', 'featured_image_url', array( // If you ...


1

'X-WP-Nonce' : Ajax.nonce Was missing that's why it was giving the error fetch('https://mywebsite.online/wp-json/wp/v2/code', { method: 'POST', credentials: 'same-origin', headers: new Headers({ 'Content-Type': 'application/json;charset=UTF-8', 'X-WP-Nonce' : Ajax.nonce }), body: JSON.stringify(OurPostData), ...


1

You can use the WordPress functions apply_filters and add_filters to store and retrieve data, like so: // The filter callback function. function so_wp_380975_get_user_args( $array ) { // (maybe) modify $array. // for now, we just return the array return [ 'meta_key' => 'mycred_default', 'orderby' => [ 'meta_value_num'...


1

your ajax post callable function action "get_news_callback" not working in your Class __construct() becuase You Can use simple Wordpress action in the class. Use Action In The Class : add_action('wp_ajax_get_news_callback', array($this, 'get_news_callback')); add_action('wp_ajax_nopriv_get_news_callback', array($this, 'get_news_callback')); your ...


1

The check_ajax_referer() function will die() if the check fails, which is why you won't see anything after it, and the reason it's failing is that the action names given to wp_create_nonce() and check_ajax_referer() do not match. Here you're using one string, 'my-string-shh': 'security' => wp_create_nonce('my-string-shh'), But here you're using another, '...


1

You should never edit any files in the wp-admin directory. All ajax related to your theme, should be inside your theme directory. You can place your PHP code in any file, functions.php, or include some new file there you will retrive only ajax cals, that will be better.


1

Add this all to your theme functions.php file and try again..


1

Seems odd to answer my own question, but in case it is of use to someone, this is the answer. To make an ajax call to a plugin, and receive the response correctly you must register that endpoint with the plugin. This allows you to sidestep the protection Wordpress is putting in your way. I'm not sure if links to YouTube are allowed/accepted on here, but ...


Only top voted, non community-wiki answers of a minimum length are eligible