Prevent wp_debug from polluting ajax within plugin

“How can I prevent unexpected output from reaching the browser/interfering with my AJAX response?” The Problem Something in your installation is generating “unexpected output,” that is, it’s creating content or data which should not be there if WordPress is running smoothly. This could imply a misconfigured server environment, a faulty WordPress installation, a bad database … Read more

get_posts empty when called via Ajax

To get data from AJAX call you have to echo or print it. In WordPress you should use wp_send_json() function to send data as response to call. function mh_get_addons( $args = array() ) { $defaults = array( ‘posts_per_page’ => -1, ‘orderby’ => ‘post_title’, ‘order’ => ‘DESC’, ‘post_type’ => ‘mh-addon’, ‘post_status’ => ‘publish’ ); $args = … Read more

Ajax – gettext without a plugin

I did manage to solve this later on by using WPML, but over time found that WPML is quite bloaty and slow, support not very good, and if you need only 1 language (but original strings for theme are in English and the language you need is not) or perhaps 2, you do not need … Read more

Conditional action hooks

During page request, WordPress actions are triggered in a sequence, therefore you have to register them in time. In your case, wp_head comes after wp_enqueue_scripts, so as a solution, i would all remove them from the wp_head and register them all in a condition, outside of any other action: if ( isset( $_COOKIE[‘user_opt’] ) ) … Read more

admin-ajax.php HTTP400: BAD REQUEST – The request could not be processed by the server due to invalid syntax

You can’t send the data to admin-ajax.php as JSON: data: JSON.stringify({ action: “process_coupon” }), For the wp_ajax_ actions to be fired, WordPress checks $_REQUEST[‘action’], but this is not populated by PHP when the form is submitted as JSON. The correct way to send the request with jQuery is to pass the data like this: data: … Read more