I’m trying to send AJAX data to my wordpress table but all I can get from my PHP is a 0
. Its on the admin side. Can anyone help me?
Also all of this code is inside my plugin-admin.php
file inside my plugin folder.
<?php
if ( ! defined( 'ABSPATH' ) || ! current_user_can( 'manage_options' ) ) exit;
global $wpdb;
global $wp_version;
$results = $wpdb -> get_results(
"
SELECT ID, post_title, post_excerpt
FROM $wpdb->posts
WHERE post_type = 'post' and post_status NOT LIKE 'auto-draft'
AND post_title NOT LIKE 'Auto Draft'
AND post_status = 'publish'
ORDER BY post_title
"
);
add_action( 'wp_ajax_featured_submit_action', 'featured_submit_callback' );
function featured_submit_callback(){
echo "hi";
wp_die();
}
?>
<div class="wrap">
<h2>Select Posts</h2>
<select id="selected-posts" multiple="multiple">
<?php
foreach ( $results as $result ){
?><option value="<?php echo $result->ID; ?>"> <?php echo $result->post_title; ?> </option> <?php
}
?>
</select>
<br>
<input type="submit" id="sposts-submit"></input>
</div>
<script>
jQuery(document).ready(function($) {
var spostsarray = new Array();
//Click button
$("#sposts-submit").click(function(){
var spostsarray = new Array();
$("#selected-posts").each(function(item){
spostsarray.push( $(this).val() );
});
console.log(spostsarray);
var data = {
"action": "featured_submit_action",
"posts": spostsarray
}
$.ajax({
url: "<?php echo admin_url('admin-ajax.php'); ?>",
type: "POST",
action: "featured_submit_action",
data: {"posts": spostsarray},
success: function(data){
console.log(data);
}
});
});
});
</script>
I’ve condensed it a bit but the general idea is that I can grab all the recent posts and the user can select which ones they want to feature, send that to the PHP method and edit the table with it.
The problem is with my AJAX callback I only ever return 0
and not the data sent from the javascript.
SOLVED:
After some help from Rohil_PHPBeginner I figured it out. The reason it didn’t work is that I was executing the code from the menu page at at that point it was too late to add a hook. Here is the page I used to solve it:
Below code worked perfectly fine for me:
You don’t need to pass the AJAX url in that way because when I used your code, it is showing me with PHP. WordPress provides a default url for AJAX so you can use that(
ajaxurl
which I used in below code).Other than that You have not added code for
no-privilege
user (if it is going to use only for privileged user then it is okay otherwise you need to add code for that).WordPress returns 0 when an ajax call doesn’t find a valid callback function (though the 0 could be return from many other things).
WordPress looks for callbacks matching
wp_ajax_{callback}
when a user is logged in andwp_ajax_nopriv_{callback}
when the user is logged out. {callback} is populated with the POST’d value of the “action” hidden input. Note that you’re not passing theaction
into your AJAX call. You should change:data: {"posts": spostsarray},
to
data: data
Since you’re not going to match a callback function without passing in action, WordPress is returning 0