I try to keep user subscription in a metadata var. The function is called by AJAX. Ahe AJAX is called but the metadata does not seem updated on frontend.

Here is the code:


<script type="text/javascript">
    jQuery(document).ready(function($) {
        $('#tags_list a').click(function(){
            myid = $(this).parent("li").attr('id');
                '<?php echo admin_url( 'admin-ajax.php' ) ?>', 
                    'action': 'change_subscription',
                    'type': 'post_tag',
                    'term_id': myid
            return false;


add_action('wp_ajax_change_subscription', 'change_subscription');
function change_subscription() {
if (!empty($_POST['term_id']) && !empty($_POST['type'])) {
    $user = wp_get_current_user();

    update_user_meta( $user->ID, 'hannit_meta', 'My Value' );
} else

Front-end check:

            $user = wp_get_current_user()->ID;
            $tags = get_user_meta( $user->ID, 'hannit_meta', true);
            echo "<!-- u:".$user." val ".$tags."-->";

Result: <!-- u:236 val-->

What am I missing here?


First You can get rid of nasty globals in your AJAX call. I'm not sure what the $st variable that you were output is all about so I've excluded, but you can include as you wish -

add_action('wp_ajax_change_subscription', 'change_subscription');
function change_subscription(){

    if(!empty($_POST['term_id']) && !empty($_POST['type'])) :

        $user_id = get_current_user_id();       
        update_user_meta( $user_id, 'hannit_meta', 'My Value' );




For testing, it looks like you were querying the meta incorrectly, so pleaes try this -

$user_id = get_current_user_id();
$tags = get_user_meta( $user_id, 'hannit_meta', true);
echo "<!-- u:".$user_id." val ".$tags."-->";

I was trying this exact method myself and was curious as to why your code wouldn't work. I changed two things; it may be one or both of them that fixed it.

Firstly, you seem to be missing a bracket after your else in the change_subscription function. Secondly; I usually err on the side of caution and use the global $current_user object:

function change_subscription() {
    if (!empty($_POST['term_id']) && !empty($_POST['type'])) {
        global $current_user;

        update_user_meta($current_user->ID, 'hannit_meta', 'My Value');

    } else {

You also had an $st variable in your second die() - I don't know if this was defined elsewhere, but I omitted it for the sake of neatness. Depending on the strictness of your setup, an undefined variable may throw a spanner in the works.

Anyway, using this exact code, this works perfectly for me. And thanks for the jumping-off point - I used your code as the basis for my own similar AJAX function.

  • You've used $user->ID but where are you getting the $user object? Oct 30 '14 at 23:24
  • @henrywright Code typo - updated.
    – indextwo
    Oct 31 '14 at 11:31
  • If you just want the current user's ID, you could probably just do get_current_user_id() and get rid of the global Oct 31 '14 at 11:43
  • @henrywright Probably. There was no explanation as to why OP's code wasn't working (nor any follow-up or acceptance of this as a solution); I simply suggested a way that probably should (and did) work in my test.
    – indextwo
    Oct 31 '14 at 11:57
  • Sure thing! Strange things happen during an AJAX call... :) Oct 31 '14 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.