admin-ajax.php slows down, but cant figure out which plugin

An option is to manually disable all plugins and enable them one-by-one and see when trouble occurse again. Another option is to use a WordPress profiler. I would highly recommend using InterconnectIT’s WordPress Performance Profiler. This plugin will profile all or a certain amount of requests and show you what plugins take a long time … Read more

Ajax return code 400

Here’s a base snippet I use for AJAX submits in WordPress, it might help you out: <?php if (isset($_POST[‘my_theme_ajax_submit’])) if ( wp_verify_nonce( $_POST[‘nonce’], ‘my_theme_ajax_submit’ ) ) my_theme_ajax_submit(); function my_theme_ajax_submit() { // do something // some php code that fires from AJAX click of #fire wp_mail( ‘[email protected]’, ‘my_theme_ajax_submit() fired’, time()); wp_die(); } ?> <button id=’fire’>Fire Something</button> … Read more

wp_query not searching with apostrophe

Try using sanitize_text_field instead of esc_html wp> $search_query = “Dalton’s Law <br/>”; string(18) “Dalton’s Law <br/>” wp> $query = apply_filters( ‘get_search_query’, $search_query ); string(18) “Dalton’s Law <br/>” wp> $esc_html = esc_html( $query ); string(29) “Dalton&#039;s Law &lt;br/&gt;” wp> $sanitized = sanitize_text_field( $query ); string(12) “Dalton’s Law” esc_html should be used to escape output before it … Read more

Use ajax from function.php

As Milo already said in comment, jQuery is in noConflict mode in WordPress. This way other JS libraries can also use $ character… One way to solve it is using jQuery everywhere in your JS files. Another is to create block and define $ variable in it: jQuery(function ($) { $.ajax(…); // here you can … Read more

WP_Query ajax loader detect end of posts

Page template: <div class=”entry-content”> <?php $args = array( ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => ‘2’, ‘paged’ => 1, ); $my_posts = new WP_Query($args); if ($my_posts->have_posts()) : ?> <div class=”my-posts”> <?php while ($my_posts->have_posts()) : $my_posts->the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_excerpt(); ?> <?php endwhile; ?> </div> <?php endif; ?> <div class=”loadmore”>Load More…</div> </div> Ajax … Read more

How to prevent those PHP variables from being cached on WordPress?

Page caching generates a static HTML version of the page so IP geolocation occurs on the first rendering and is not triggered on subsequent renderings – your use case is not compatible with W3 Total Cache’s page caching. It’s possible to accomplish what you are trying to accomplish with page caching, however, you would need … Read more

Ajax $wpdb not returning table data

The main problem, I see in your code, is that you use query method of wpdb and assume that it is PDO statement object… But it is not. Take a look at wpdb Codex: https://codex.wordpress.org/Class_Reference/wpdb query method is used to run custom queries on DB – that’s all. What you want to use is get_results. … Read more

AJAX filter posts by year

Try by change appropriate code: Front end <form action=”<?php echo site_url()?>/wp-admin/admin-ajax.php” method=”POST” id=”filter> <select name=”year”></select> <input type=”hidden” name=”action” value=”filter” /> </form> JS ajax request $.ajax({ url : filter.attr(‘action’) , data : filter.serialize() , type : filter.attr(‘method’) , success : function (response) { console.log(response) }, error : function (request , status , error) { console.log(request.responseText) console.log(error) … Read more

Implementing an AJAX POST API call in wordpress

You got the error 400 Bad Request because the Content-Type header is application/json: $.ajax({ headers:{ “Content-Type”:”application/json” }, data: { … }, … }); And when the request content is a JSON string, the $_REQUEST[‘action’] which WordPress uses to identify the proper AJAX action (which is foo in your code) is not available (i.e. it’s a … Read more