I asked a similar question with good advise to help me, but i have run into another problem with the same code. What I am looking to do is simply click a button on a users profile and change the default "0", meaning they're not banned, to a "1". Before I was using a hook and a "do_action" with no problems, now I am trying to use ajax. I will start by posting my code.
FUNCTION
add_action( 'wp_ajax_remove_account', 'remove_account' );
function remove_account() {
global $wpdb;
global $current_user;
$author = get_queried_object();
$uid = $author->ID;
$user = $current_user->ID;
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->users SET removed = '%d' WHERE ID = '%d'", '1', $user ));
wp_die();
}
AJAX
jQuery(document).ready(function($) {
$('.wordpress-ajax-form').on('submit', function(e) {
e.preventDefault();
var $form = $(this);
$.post($form.attr('action'), $form.serialize(), function(data) {
}, 'json');
}); });
FORM
<form class="wordpress-ajax-form" method="post" action="<?php echo AJAX_URL; ?>" >
<input type="hidden" name="action" value="remove_account" ><br>
<center><button id="msgbutton">Remove Account</button></center>
</form>
The above will work fine, however if I change
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->users SET removed = '%d' WHERE ID = '%d'", '1', $user ));
to
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->users SET removed = '%d' WHERE ID = '%d'", '1', $uid ));
to get the user id of the profile I'm looking at, it doesn't work.
I added
$author = get_queried_object();
echo $author->ID;`
to my header and it gave me back the user id of the profile i was looking at so I know that code works. Any help is appreciated.