WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

is there an easy way for me to save a variable and link it to a user?

specifically I want to use meta box to output a variable on the front page, then have it be a link so if a user clicks it, it will store it in a div. I can do all the meta box stuff, just need the variable to be stored when link is clicked. kind of like a favorite system, but more custom. assuming I will have to access database somehow? not even sure where to start.

share|improve this question
Did you ever find an answer on this? – Brian Fegter Feb 24 '12 at 16:55

To expound on Chris' answer, the AJAX call would look something like this in your functions.php file:

add_action('wp_ajax_add_favorite', 'add_favorite');

function add_favorite(){
    $post_id = $_POST['postID'];
    $nonce = $_POST['nonce'];
    if(!wp_verify_nonce($nonce, 'wpnonce'))

        $favorites = get_user_meta(get_current_user_id(), 'favorites');
        if(in_array($post_id, $favorites)):
            $response = 'Already in favorites.';
            $success = false;
            if(add_user_meta(get_current_user_id(), 'favorites', $post_id, false)):
                $response = "This post was added to your favorites";
                $success = true;
        $success = false;
        $response = "No post ID was supplied";

    $output = json_encode(array('success'=>$success, 'response'=>$response));
    echo $output;

The jQuery AJAX request would look like this:


    var data = {
        action: 'add_favorite',
        nonce: jQuery('#_wpnonce').val(),
        postID: jQuery(this).attr('rel')

    jQuery.post(ajaxurl, data, function(output) {
        var obj = jQuery.parseJSON(output);
        if(!obj.response) alert('Something went wrong with the server.');

            //Do Something Here

    //Disable Click Through
    return false;

Your link on the front-end would look like this:

<a class="add-to-favorites' rel="<?php echo $post->ID;?>" href="#">Add To Favorites</a>

Add a hidden nonce field to the called 'wpnonce' and this should work just dandy. Also, make sure you define ajaxurl on the frontend so the script knows where to send the request.

You will need to do some reverse engineering in another PHP AJAX function to remove from favorites though. Hope this helps you out.

share|improve this answer

You can use update_user_meta to store the data, and get_user_meta to retrieve the data. As far as actually saving it when clicked, you could use a simple ajax call when the user clicks the link. Check out the following resources for dealing with AJAX in Wordpress.

AJAX in Wordpress AJAX in Plugins

share|improve this answer
perfect. thanks! – lukasz Aug 9 '11 at 19:50

Your Answer


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

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