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.

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
add comment

2 Answers

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
add comment

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'))
        exit;

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

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

The jQuery AJAX request would look like this:

jQuery('.add-to-favorites').click(function(){

    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.');

        if(obj.success){
            //Do Something Here
            alert(obj.response);
        }
        else{
            alert(obj.response);
        }
    });

    //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
add comment

Your Answer

 
discard

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.