WordPress ajax-action failing because of newline in response
Found a plugin ending with ?> with a newline after it
Found a plugin ending with ?> with a newline after it
may be here is the error add_action( ‘wp_ajax_my_ajax_action’, ‘my_ajax_action_callback’ ); add_action( ‘wp_ajax_nopriv_my_ajax_action’, ‘my_ajax_action_callback’ ); change to add_action( ‘wp_ajax_my_ajax_action_callback’, ‘my_ajax_action_callback’ ); add_action(‘wp_ajax_nopriv_my_ajax_action_callback’,’my_ajax_action_callback’ ); and in ajax change: action : ‘my_ajax_action_callback go to functions.php and add: require(dirname(__FILE__) . ‘/custom_template.php’); make sure your ajax url is correct!!
I love to solve problems and to keep that habit fresh I’ve solved your problem. There were lots of issues. Now the form and ajax is working perfectly but still, there are some issues. For instance, you didn’t use nonce or CSRF token, which will lead to CSRF. You didn’t use any validation which is … Read more
I believe the problem is here: // handle AJAX calls for both logged in and guest users add_action(‘wp_action_websites_cpt_create_post’, [$this, ‘create_post’]); add_action(‘wp_action_nopriv_websites_cpt_create_post’, [$this, ‘create_post’]); Because the format should be: add_action( ‘wp_ajax_{ACTION_NAME}’, [ $this, ‘create_post’ ] ); add_action( ‘wp_ajax_nopriv_{ACTION_NAME}’, [ $this, ‘create_post’ ] ); ..where {ACTION_NAME} is the “action” name, e.g. websites_cpt_create_post. See wp_ajax_{$_REQUEST[‘action’]} and wp_ajax_nopriv_{$_REQUEST[‘action’]}.
Plugins and your theme are loaded on every request, including AJAX, so any code in them is parsed. They need to be so that any AJAX hooks are found and executed. Most of the code isn’t executed though, because it’s in functions that are hooked to hooks that aren’t executed during an AJAX request. Some … Read more
In very broad terms, each ajax request takes the same amount of “effort” by the server to process like a full page load. From this perspective, 30 AJAX requests instead of one page load sounds like a very bad idea. It all also depends on the frequency those requests are being “fired”, if it happens … Read more
Look at the URL in the error: http://solucase.com/wp-json/wp/v2/posts?categories=%27cat_id%27 %27 is the URL encoded character for ‘, so you can see what’s happening is that in the URL you are requesting with AJAX cat_id is not being replaced with the variable value. This is because the way you have used quotes when defining the URL is … Read more
I’m having some doubts about your code. IF your using WordPress you should use https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action) So you no session.php, but a function: add_action( ‘wp_ajax_foobar’, ‘my_ajax_foobar_handler’ ); function my_ajax_foobar_handler() { $u = $_POST[“url”]; $p = $_POST[“product”]; echo $u . ” ” .$p ; wp_die(); } and simplify yours JS jQuery.post( my_foobar_client.ajaxurl, { ‘action’: ‘foobar’, ‘url’ : … Read more
I am calling get_cat() function from update_passage_list() function This is what you’re doing: $cpid = get_cat($cpid); There’s nothing there that says that get_cat() is a method of your class. It’s trying to find a function get_cat() outside any class. To use the method of the class you need to use $this: $cpid = $this->get_cat($cpid);
Try using wp_doing_ajax() like so: function unset_filter_session() { if ( ! wp_doing_ajax() ) { //Reset sessions on refresh page unset( $_SESSION[‘expenditure_filter’] ); } } UPDATE You can check the answer’s revision for this update part.. UPDATE #2 Sorry, I didn’t realize that you’re loading a page fragment (#content-area) using the jQuery.load() method. Or that you’re … Read more