Targeting single page with JS

Here, the code operates on anything that matches the #searchButton element: jQuery(“#searchButton”) If there is a searchButton on other pages, it will match it, soo you need to be more specific. For example, on the homepage, the <body> tag will have the home class, here is my sites homepage body tag: <body class=”home blog logged-in … Read more

Search – Ajax – Alter Query Parameters with Pagination

I think this is what you want. base is set via home_url(), format is page/%#%/, search query arg is added via add_args if it exists: $args = array( ‘base’ => home_url( ‘/%_%’ ), ‘format’ => ‘page/%#%/’, ‘current’ => max( 1, get_query_var(‘paged’) ), ‘total’ => $temp->max_num_pages, ); if( isset($_GET[‘s’]) ){ $args[‘add_args’] = array( ‘s’ => $_GET[‘s’] … Read more

How to include admin-ajax when loading external javascript

What you need to do is register and/or enqueue your script and then use wp_localize_script to include a Javascript variable on your page. The Codex page has this example: wp_enqueue_script( ‘some_handle’ ); $translation_array = array( ‘some_string’ => __( ‘Some string to translate’ ), ‘a_value’ => ’10’ ); wp_localize_script( ‘some_handle’, ‘object_name’, $translation_array ); You can then … Read more

Turn jQuery.ajax() request into XMLHttpRequest (vanilla JavaScript)

Here’s my take on it, although a bit late, but I am working on converting all jQuery.ajax() calls to vanilla JavaScript, so why not. // alpha JS request // no jQuery // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send#Example_POST // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest var request = new XMLHttpRequest(); request.open(‘POST’, fpm_ajax.ajax_url, true); request.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded;’); request.onload = function () { if (this.status >= 200 && … Read more

WP_Query class not found

That’s because when calling a theme template directly you only have what’s included/defined in the template file, not the whole WordPress environment. Some people attempt to fix this by adding an include of wp-header or load etc but this is incredibly damaging When doing AJAX requests, never call a file in the theme, instead call … Read more

How to “Load More” posts via AJAX?

You can use ajax to load more posts on your archive page. attach a js/jquery click event on the More link send ajax request to admin-ajax.php (use wp_localize_script to get the ajax url to front end) with page number (track this with js variable). handle ajax request in php. Add custom ajax actions with add_action( … Read more