Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to update some user meta information from the front end of a WordPress page when the user clicks on a button. I am attempting to pass along a string of data, and then updating the field when the data is passed to the php file.

This is the code I have, but the data is not being updated on the user profile page.

Ajax

jQuery(document).on('click', ".removeFav", function() {

    var item = '#'+jQuery(this).parent("div").attr("id");
    jQuery(item).remove(item); //visually remove item from product list

    // AJAX call to remove item
    jQuery.ajax({
        url: '<?php echo get_template_directory_uri(); ?>/my-product-delete.php', //using data-slug value to specify unique product id + data-category for tab category (so item isn't deleted across all tabs if there are duplicates)
        type: 'post',
        data: { 'action' : 'change_json_favoriteString', 'id' :  jQuery(this).parent("div").attr("data-slug") , 'category' :  jQuery(this).parent("div").attr("data-category") },
        dataType: 'json',
        success: function(data) { 
            alert("Deleted");
            alert(JSON.stringify(data));
        }, 
        error: function(data){
            alert("error");
        }
    });
});

my-product-delete.php

<?php


    $thisProductID = $_GET['id'];
    $thisProductCategory = $_GET['category'];

    /* Get current user info */
    get_currentuserinfo(); 

    $currentUserID = $current_user->ID;

    $currentUsersFavorites = $current_user->json_favorites;

    $newJSONfavorites = 'http://wordpress.org'; // <= must create a dynamic variable to populate with new favorites json object
    update_user_meta($currentUserID, 'json_favorites', $newJSONfavorites); // <= update users json favorites with new json object



?>
share|improve this question
    
What have you done to debug this? –  s_ha_dum Dec 8 '13 at 2:25
1  
You got the wrong ajax url: it should point to wp-admin/admin-ajax.php and not to my-product-delete.php'. also there are other things you are doing wrong here. check this tutorial for the right way wp.smashingmagazine.com/2011/10/18/how-to-use-ajax-in-wordpress –  Maruti Mohanty Dec 8 '13 at 7:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.