Ajax call theme functions from front

I think that your problem is ajaxurl is not available for your script. Try to defined that varibale before your js code is loaded. The best way is using wp_localize_script: The PHP: add_action( ‘wp_enqueue_scripts’, ‘cyb_enqueue_scripts’ ); function cyb_enqueue_scripts() { //Change the key and url with yours wp_register_script(‘my-js’, get_stylesheet_directory_uri(). ‘/js/my-js.js’, array( ‘jquery’ ) ); wp_enqueue_script(‘my-js’); //Localize … Read more

add_action and Ajax

I skimmed through the plugin’s code and you could try to use the define_public_hooks() method of the Plugin_Name class to register your ajax action callbacks of the Plugin_Name_Public class: /** * Register all of the hooks related to the public-facing functionality * of the plugin. * * @since 1.0.0 * @access private */ private function … Read more

How to load custom post archives sub-pages with ajax?

you might use the $.load and preventDefault() from jQuery. Assugming that you have .page-link link class on each url in pagination and you have all the posts in the #content div following code can help you to load the posts in that div on that particular page. $(“body”).on(“click”, “a.page-link”, function(e) { e.preventDefault(); $(“#content”).load($(this).attr(“href”) + ” … Read more

Async Loading of Custom Posts

Testing is the only way to be sure, but my guess would be that it is not the query itself that is slowing the page load. After you have fetched those 2000 records, you are looping through them. Every time you call get_template_part. This function in turn calls locate_template, which leads to load_template, where you … Read more

Allow AJAX call to other roles than admin

All the WordPress AJAX calls should be handled by the admin-ajax.php, wether they happen on the frontend or in the backend. To grant the access you have to register the callbackfuntion for the AJAX call add those lines to your file: add_action( ‘wp_ajax_prefix_update_post’, ‘prefix_update_post’ ); add_action( ‘wp_ajax_nopriv_prefix_update_post’, ‘prefix_update_post’ ); Be sure to add some validation … Read more

Why would admin-ajax.php redirect to the home page for logged out users?

It only redirects when accessed directly, as do all files located in wp-admin/. AJAX requests should work fine regardless of authentication status. Edit: wp-admin/admin-ajax.php should not redirect in any situation. Perhaps a plugin is redirecting all unauthenticated users to the homepage? By default, accessing files inside wp-admin/ when not logged in should redirect to the … Read more

WooCommerce: Translation lost on AJAX call in Checkout page [closed]

I think I fixed the issue with the following extended function in my plugin: function localize_backend( $locale ) { // set langauge if user is in admin area if( defined( ‘WP_ADMIN’ ) || ( isset( $_REQUEST[‘pwd’] ) && isset( $_REQUEST[‘kau-boys_backend_localization_language’] ) ) ){ // ajax call from frontend if ( ‘admin-ajax.php’ == basename( $_SERVER[ ‘SCRIPT_FILENAME’ … Read more

wp_ajax() question.. not using wp_enqueue_script?

Ok, firstly let me just break it down a little, so you understand the purpose of these functions. wp_enqueue_script() Description: A safe way of adding javascripts to a WordPress generated page. Basically, include the script if it hasn’t already been included, and load the one that WordPress ships. wp_localize_script() Description: Localizes a script, but only … Read more

Help with shortcode in admin-ajax [closed]

How you are applying a filter is wrong. You need to bind a function to the filter, but you are providing a variable. function my_filter( $content ) { return $content . ‘My added code’; } add_filter( ‘the_content’, ‘my_filter’ ); You should add the code like this, this should work.

WordPress Ajax Getting Response “Null”

You are rendering the json data the wrong way. Change the success to the below:– success: function(data) { alert(data.id); // Now you can similarly render the other details like details.success }