How can i do public ajax call?
Question
I need to know exactly how the AJAX URL REQUEST works in wordpress
my code :
php (in function.php):
add_action('wp_ajax_nopriv_wpa56343_search_widget', 'wpa56343_search_widget'); // for not logged in users
add_action('wp_ajax_wpa56343_search_widget', 'wpa56343_search_widget');
function wpa56343_search_widget()
{
global $wp_query;
$search = $_POST['query'];
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
//'posts_per_page' => -1,
's' => $search,
'posts_per_page' => 20,
/*'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => '1',
),
),*/
);
$wp_query = new WP_Query( $args );
if($wp_query->have_posts())
while($wp_query->have_posts()): $wp_query->the_post(); ?>
<h1>do some html work</h1>
<?php endwhile;
wp_reset_postdata();
//endif;
die();
}
my simple SCRIPT code
$(document).ready(function(){
$('#s-mobileapp').keypress(function(event){
var query = $(this).val();
if(query.length > 2)
{
$.ajax({
type:"POST",
url: "./wp-admin/admin-ajax.php",
data: {
action:'wpa56343_search_widget_mobileapp',
query:query,
},
success:function(data){
$('#searchwidgetresult-mobileapp').html(data);
$('#searchwidgetresultcontainer-mobileapp').css({"display":"block","border":"1px solid rgb(187, 187, 187)","box-shadow":"0px 2px 9px rgba(0, 0, 0, 0.4)"});
}
});
}else{
$('#searchwidgetresult-mobileapp').html('');
$('#searchwidgetresultcontainer-mobileapp').hide();
}
}).blur(function() {
setTimeout(function() {
$('#searchwidgetresultcontainer-mobileapp').hide()
}, 500);
}).focus(function() {
$('#searchwidgetresultcontainer-mobileapp').show();
});
});
The problem is these functions works good in some pages and some not.
when i test the SCRIPT code by alert i found that the code stops in the ajax url call
url: "./wp-admin/admin-ajax.php",
so is there any other way for this url method ?
something like public ajax call ?
0
2 years
2020-12-17T21:10:26-05:00
2020-12-17T21:10:26-05:00 0 Answers
7 views
0
Leave an answer