Post Meta deletion ajax not working

I am trying to remove function in the backend of post like plugin. In the post like plugin they are storing values in the following manner that is User Id , Post id and comment (or) Post . post_user_likes( $user_id, $post_id, $is_comment );

I am trying to keep a delete button to a particular user to remove his like through ajax While doing that i am getting deleted message in the console but like not deleted from the user profile area. I am Attaching the link of the plugin .https://github.com/JonMasterson/WordPress-Post-Like-System/blob/master/post-like.php

Here is my coading what i did :

Called ajaxurl in the header.

function testdel( $post ) { // note the $post varaible as argument
wp_nonce_field(‘testdel’, ‘ajaxsecurity’); // is a good practise adding nonces
?>
<input type=”hidden” value=”<?php echo $post->ID; ?>” id=”ajaxtestdel_postid”>
<th scope=”row”><label for=”del”>delete test key</label></th>
<td><input type=”button” name=”del” id=”del” value=”delete” class=”button”></td>
<script>
jQuery(‘#del’).on(‘click’, function(){
var $this = jQuery(this);
var post = jQuery(‘#ajaxtestdel_postid’).val(); // get post id from hidded field
var nonce = jQuery(‘input[name=”ajaxsecurity”]’).val(); // get nonce from hidded field
jQuery.ajax({
url: ajaxurl, // in backend you should pass the ajax url using this variable
type: ‘POST’,
data: { action : ‘ajaxtestdel’, postid: post, ajaxsecurity: nonce },
success: function(data){
console.log(data);
$this.val(‘deleted’);
}
});
});
</script>
<?php
}

function ajaxtestdel() {
$postid = isset($_POST[‘postid’]) ? $_POST[‘postid’] : ”;
//$postid=’382′;
$nonce = isset($_POST[‘ajaxsecurity’]) ? $_POST[‘ajaxsecurity’] : ”;
if ( $postid && $nonce && wp_verify_nonce($nonce, ‘testdel’) ) {
$status = delete_post_meta($postid, ‘_post_like_count’) ? ‘Error’ : ‘Success’;
} else {
$status = ‘Error’;
}
die($status);
}

add_action(‘wp_ajax_ajaxtestdel’, ‘ajaxtestdel’);

Thank’s For your help !!!

Read more here:: Post Meta deletion ajax not working

Leave a Reply

Your email address will not be published. Required fields are marked *