How to link WordPress heartbeat to ajax form

Saving data Based on Heartbeat API, you can add custom data to the sent heartbeat on heartbeat-send event in js. This would be used instead of the ajax function you have in your code example. jQuery( document ).on( ‘heartbeat-send’, function ( event, data ) { // Grab the required form data data.location = jQuery(‘input[name=”location”]:checked’).val(); data.userid … Read more

jQuery Autocomplete in WordPress

don’t include WP like that. use $_GET instead: … $(“#tags”).autocomplete(“<?php echo add_query_arg(‘get_my’, ‘terms’, home_url()); ?>”, … theme’s functions.php: add_action(‘template_redirect’, ‘terms_for_autocomplete’); function terms_for_autocomplete(){ if(isset($_GET[‘get_my’]) && $_GET[‘get_my’] == ‘terms’): $terms = &get_terms(get_taxonomies()); foreach ($terms as $term) echo “{$term->name}|{$term->name} ({$term->count} results)\n”; die(); endif; }

admin-ajax.php vs .load() in WordPress

Is it the only way to ajaxify your whole site or if there is any better way? No, it’s not the only way. As you already know, you can indeed use .load(). As to what’s better? That’s wide open to debate and depends on personal preference, requirements, circumstances etc. Cannot we use .load() method with … Read more

How do I send an ajax request and get a response back from a function?

$.ajax is very simple and you can append more parameters easily in the data: {action:’my_test_ajax’} line, try this: var target=”http://” + window.location.hostname + ‘/wp-admin/admin-ajax.php’; $.ajax({ url: ajax, data: {action:’my_test_ajax’}, type: ‘post’, success: function(data){ console.log(data, data.title, data.content) } }); Make sure the target points exactly to admin-ajax.php or set a global variable (or using localize script) … Read more

WordPress embeds in AJAX inserted content

I think the solution is amazingly simple here: You’re just missing a single line of code in your ajax callback, namely: global $post; where I assume you’re using: $post = get_post( $post_id ); The reason is that there’s a global post check in the WP_Embed::shortcode() method. More details in my answer here.

Cannot load admin-ajax.php. No access-control allow origin*

There are filters for allowed_http_origins and add_allowed_origins. You can use them to set the proper Access-Control-Allow-Origin header in the response to your AJAX call. Add this to your theme’s functions.php file: add_filter(‘allowed_http_origins’, ‘add_allowed_origins’); function add_allowed_origins($origins) { $origins[] = ‘https://www.yourdomain.com’; return $origins; }

Basic question on passing variables to AJAX in WordPress

The simple answer: Don’t bother with the Admin AJAX API, use the REST API! Tell WordPress about your endpoint: add_action( ‘rest_api_init’, function () { // register dongsan/v1/whatever register_rest_route( ‘dongsan/v1’, ‘/whatever/’, array( ‘methods’ => ‘POST’, // use POST to call it ‘callback’ => ‘dongsan_whatever’ // call this function ) ); } ); Now we have an … Read more