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.

We're creating a site where we want guest users to add pages to a favourites list (post IDs), so they can print or email their selection if they want.

What would the best way be to do this in WordPress? I think ajax but I'm not too sure how to save it in the WordPress framework.

share|improve this question
2  
you can do this with javascript only, and cookies (unless you need to send that email from your site, which is probably not a good idea to do for guests) –  onetrickpony Jun 23 '11 at 17:54
    
I appreciate this something some sites provide, but isn't it just recreating existing browser functionality? I never saw the point much in that - although that might just be me. –  ZweiBlumen Dec 1 '11 at 9:50
add comment

2 Answers

Cookies, or even HTML5 local storage, seem like a good way to implement this. Here's some basic code that could serve as a starting point. Post IDs are stored as CSV in the cookie.

// Load current favourite posts from cookie
$favposts = (isset($_COOKIE['favposts'])) ? explode(',', (string) $_COOKIE['favposts']) : array();
$favposts = array_map('absint', $favposts); // Clean cookie input, it's user input!

// Add (or remove) favourite post IDs
$favposts[] = $post->ID;

// Update cookie with new favourite posts
$time_to_live = 3600 * 24 * 30; // 30 days
setcookie('favposts', implode(',', array_unique($favposts)), time() + $time_to_live);
share|improve this answer
add comment

You can save the information directly to a database table, so you'd have a table of userIDs associated with postIDs, and to get the list of posts that user has favorited, you can run a query on the database.

See: Class Reference/wpdb

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.