Returning JSON data with ajax in wordpress -
ok long question here. i'm new ajax , using in context of wordpress, i've been following along tutorials online , think i'm there.
i'll paste have far , explain thinking.
ok start, js.
jquery(document).ready(function(){ jquery('.gadgets-menu').mouseenter(function(){ doajaxrequest(); }); }); mouse enters .gadgets-menu , request triggers, using mouseenter fires once.
the request itself.
function doajaxrequest(){ // here request happen jquery.ajax({ url: 'http://www.mysite.com/wp-admin/admin-ajax.php', data:{ 'action':'do_ajax', 'fn':'get_latest_posts', 'count':5 }, datatype: 'json', success:function(data){ //here don't know do. }, error: function(errorthrown){ alert('error'); console.log(errorthrown); } }); } now php function.
add_action('wp_ajax_nopriv_do_ajax', 'our_ajax_function'); add_action('wp_ajax_do_ajax', 'our_ajax_function'); function our_ajax_function(){ switch($_request['fn']){ case 'get_latest_posts': $output = ajax_get_latest_posts($_request['count']); break; default: $output = 'no function specified, check jquery.ajax() call'; break; } $output=json_encode($output); if(is_array($output)){ print_r($output); } else{ echo $output; } die; } and ajax_get_latest_posts function
function ajax_get_latest_posts($count){ $posts = get_posts('numberposts='.'&category=20'.$count); return $posts; } so, if i've done right output should $posts = get_posts('numberposts='.'&category=20'.$count); ie. number of posts (5), category 20. don't know now, how title , thumbnail?
i'm sorry if silly, i'm fumbling around here.
amended php
add_action('wp_ajax_nopriv_do_ajax', 'our_ajax_function'); add_action('wp_ajax_do_ajax', 'our_ajax_function'); function our_ajax_function(){ $output = ajax_get_latest_posts($_request['count']); // or $_get['count'] if($output) { echo json_encode(array('success' => true, 'result' => $output)); } else { wp_send_json_error(); // {"success":false} // similar to, echo json_encode(array("success" => false)); // or can use, - // echo json_encode(array('success' => false, 'message' => 'not found!')); } $output=json_encode($output); if(is_array($output)){ print_r($output); } else{ echo $output; } die; } function ajax_get_latest_posts($count) { $args = array( 'numberposts' => $count, 'order' => 'desc','category' => 20 ); $post = wp_get_recent_posts( $args ); if( count($post) ) { return $post; } return false; } this not work.
jquery(document).ready(function(){ jquery('.gadgets-menu').mouseenter(function(){ doajaxrequest(); }); }); function doajaxrequest(){ // here request happen jquery.ajax({ url: 'http://localhost:8888/wp-admin/admin-ajax.php', data:{ 'action':'do_ajax', 'fn':'get_latest_posts', 'count':5 }, datatype: 'json', success:function(data){ if(data.success) { alert("it works"); } else { // alert(data.message); // or whatever... } } }); } no alert shown.
in code get_posts('numberposts='.'&category=20'.$count); wrong, can use wp_get_recent_posts function instead (though uses get_posts anyway), example
function ajax_get_latest_posts($count) { $args = array( 'numberposts' => $count, 'order' => 'desc','category' => 20 ); $post = wp_get_recent_posts( $args ); if( count($post) ) { return $post; } return false; } then in our_ajax-function can use
$output = ajax_get_latest_posts($_request['count']); // or $_get['count'] if($output) { echo json_encode(array('success' => true, 'result' => $output)); } else { wp_send_json_error(); // {"success":false} // similar to, echo json_encode(array("success" => false)); // or can use, - // echo json_encode(array('success' => false, 'message' => 'not found!')); } in success callback function, can check
success:function(data){ if(data.success) { // loop array, , whatever want $.each(data.result, function(key, value){ // can use $(this) // console.log($(this)); // check debug , idea }); } else { // alert(data.message); // or whatever... } } you can read here wp_send_json_error helper function learn more helper functions.
update :
also remember that, after $output=json_encode($output); $output not array anymore, instead, it's json string, is_array($output) return false if use is_array() before encode using $output=json_encode($output); like
if( is_array( $output ) ) { $output = json_encode( $output ); } in case, is_array( $output ) return true.
Comments
Post a Comment