<?php wpfp_link(); ?> where ever you want on a single post. Then create a page which includes that text : {{wp-favorite-posts}} That's it! Version: 1.4.3 Author: Huseyin Berberoglu Author URI: http://nxsn.com */ /* Copyright (c) 2009 Hüseyin Berberoğlu (hberberoglu@gmail.com) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ define('WPFP_PATH', WP_PLUGIN_URL . '/wp-favorite-posts'); define('WPFP_META_KEY', "wpfp_favorites"); define('WPFP_USER_OPTION_KEY', "wpfp_useroptions"); define('WPFP_COOKIE_KEY', "wp-favorite-posts"); $ajax_mode = 1; function wp_favorite_posts() { $wpfp_options = wpfp_get_options(); if (isset($_REQUEST['wpfpaction'])): global $ajax_mode; $ajax_mode = $_REQUEST['ajax']; if ($_REQUEST['wpfpaction'] == 'add') { wpfp_add_favorite(); } else if ($_REQUEST['wpfpaction'] == 'remove') { wpfp_remove_favorite(); } else if ($_REQUEST['wpfpaction'] == 'clear') { if (wpfp_clear_favorites()) wpfp_die_or_go($wpfp_options['cleared']); else wpfp_die_or_go("ERROR"); } endif; } add_action('template_redirect', 'wp_favorite_posts'); function wpfp_add_favorite($post_id = "") { $wpfp_options = wpfp_get_options(); if ( empty($post_id) ) $post_id = $_REQUEST['postid']; if ($wpfp_options['opt_only_registered'] && !is_user_logged_in() ) wpfp_die_or_go($wpfp_options['text_only_registered']); if (wpfp_do_add_to_list($post_id)) { // added, now? do_action('wpfp_after_add', $post_id); if ($wpfp_options['statics']) wpfp_update_post_meta($post_id, 1); if ($wpfp_options['added'] == 'show remove link') { $str = wpfp_link(1, "remove", 0, array( 'post_id' => $post_id ) ); wpfp_die_or_go($str); } else { wpfp_die_or_go($wpfp_options['added']); } } } function wpfp_do_add_to_list($post_id) { if (wpfp_check_favorited($post_id)) return false; if (is_user_logged_in()) { return wpfp_add_to_usermeta($post_id); } else { return wpfp_set_cookie($post_id, "added"); } } function wpfp_remove_favorite($post_id = "") { $wpfp_options = wpfp_get_options(); if (empty($post_id)) $post_id = $_REQUEST['postid']; if (wpfp_do_remove_favorite($post_id)) { // removed, now? do_action('wpfp_after_remove', $post_id); if ($wpfp_options['statics']) wpfp_update_post_meta($post_id, -1); if ($wpfp_options['removed'] == 'show add link') { if ($_REQUEST['page']==1): $str = ''; else: $str = wpfp_link(1, "add", 0, array( 'post_id' => $post_id ) ); endif; wpfp_die_or_go($str); } else { wpfp_die_or_go($wpfp_options['removed']); } } else return false;; } function wpfp_die_or_go($str) { global $ajax_mode; if ($ajax_mode): die($str); else: wp_redirect($_SERVER['HTTP_REFERER']); endif; } function wpfp_add_to_usermeta($post_id) { $wpfp_favorites = array(); $wpfp_favorites = wpfp_get_user_meta(); $wpfp_favorites[] = $post_id; wpfp_update_user_meta($wpfp_favorites); return true; } function wpfp_check_favorited($cid) { if (is_user_logged_in()): $favorite_post_ids = wpfp_get_user_meta(); if ($favorite_post_ids) foreach ($favorite_post_ids as $fpost_id) if ($fpost_id == $cid) return true; endif; if (wpfp_get_cookie()): foreach (wpfp_get_cookie() as $fpost_id => $val) if ($fpost_id == $cid) return true; endif; return false; } function wpfp_link( $return = 0, $action = "", $show_span = 1, $args = array() ) { global $post; $post_id = $post->ID; extract($args); if ($show_span) $str = ""; $str .= wpfp_before_link_img(); $str .= wpfp_loading_img(); $wpfp_options = wpfp_get_options(); if ($action == "remove"): $str .= wpfp_link_html($post_id, $wpfp_options['remove_favorite'], "remove"); elseif ($action == "add"): $str .= wpfp_link_html($post_id, $wpfp_options['add_favorite'], "add"); elseif (wpfp_check_favorited($post_id)): $str .= wpfp_link_html($post_id, $wpfp_options['remove_favorite'], "remove"); else: $str .= wpfp_link_html($post_id, $wpfp_options['add_favorite'], "add"); endif; if ($show_span) $str .= ""; if ($return) { return $str; } else { echo $str; } } function wpfp_link_html($post_id, $opt, $action) { return "". $opt .""; } function wpfp_get_users_favorites($user = "") { $favorite_post_ids = array(); if (!empty($user)): return wpfp_get_user_meta($user); endif; # collect favorites from cookie and if user is logged in from database. if (is_user_logged_in()): $favorite_post_ids = wpfp_get_user_meta(); endif; if (wpfp_get_cookie()): foreach (wpfp_get_cookie() as $post_id => $post_title) { array_push($favorite_post_ids, $post_id); } endif; return $favorite_post_ids; } function wpfp_list_favorite_posts( $args = array() ) { $user = $_REQUEST['user']; extract($args); $wpfp_options = wpfp_get_options(); if (!empty($user)): if (!wpfp_is_user_favlist_public($user)): $favorite_post_ids = wpfp_get_users_favorites($user); endif; else: $favorite_post_ids = wpfp_get_users_favorites(); endif; if (@file_exists(TEMPLATEPATH.'/wpfp-page-template.php')): include(TEMPLATEPATH.'/wpfp-page-template.php'); else: include("wpfp-page-template.php"); endif; } function wpfp_loading_img() { return "Loading"; } function wpfp_before_link_img() { $options = wpfp_get_options(); $option = $options['before_image']; if ($option == '') { return ""; } else if ($option == 'custom') { return "Favorite"; } else { return "Favorite"; } } function wpfp_clear_favorites() { if (wpfp_get_cookie()): foreach (wpfp_get_cookie() as $post_id => $val) { wpfp_set_cookie($post_id, ""); wpfp_update_post_meta($post_id, -1); } endif; if (is_user_logged_in()) { $favorite_post_ids = wpfp_get_user_meta(); if ($favorite_post_ids): foreach ($favorite_post_ids as $post_id) { wpfp_update_post_meta($post_id, -1); } endif; if (!delete_usermeta(wpfp_get_user_id(), WPFP_META_KEY)) { return false; } } return true; } function wpfp_do_remove_favorite($post_id) { $a = true; if (is_user_logged_in()) { $user_favorites = wpfp_get_user_meta(); $user_favorites = array_diff($user_favorites, array($post_id)); $user_favorites = array_values($user_favorites); $a = wpfp_update_user_meta($user_favorites); } if ($a) $a = wpfp_set_cookie($_REQUEST['postid'], ""); return $a; } function wpfp_content_filter($content) { if (is_page()): if (strpos($content,'{{wp-favorite-posts}}')!== false) { return str_replace('{{wp-favorite-posts}}', wpfp_list_favorite_posts(), $content); } endif; #if (is_single()): if (strpos($content,'[wpfp-link]')!== false) { return str_replace('[wpfp-link]', wpfp_link(1), $content); } #endif; return $content; } add_filter('the_content','wpfp_content_filter',7); function wpfp_shortcode_func() { wpfp_list_favorite_posts(); } add_shortcode('wp-favorite-posts', 'wpfp_shortcode_func'); function wpfp_add_js_script ( ) { echo '' . "\n"; wp_enqueue_script( "wp-favroite-posts", WPFP_PATH . "/wpfp.js", array( 'jquery' ) ); } add_action('wp_print_scripts', 'wpfp_add_js_script'); function wpfp_init() { $wpfp_options = array(); $wpfp_options['add_favorite'] = "Add to favorites"; $wpfp_options['added'] = "Added to favorites!"; $wpfp_options['remove_favorite'] = "Remove from favorites"; $wpfp_options['removed'] = "Removed from favorites!"; $wpfp_options['clear'] = "Clear favorites"; $wpfp_options['cleared'] = "

Favorites cleared!

"; $wpfp_options['favorites_empty'] = "Favorite list is empty."; $wpfp_options['cookie_warning'] = "Your favorite posts saved to your browsers cookies. If you clear cookies also favorite posts will be deleted."; $wpfp_options['rem'] = "remove"; $wpfp_options['text_only_registered'] = "Only registered users can favorite!"; $wpfp_options['statics'] = 1; $wpfp_options['widget_title'] = ''; $wpfp_options['widget_limit'] = 5; $wpfp_options['before_image'] = 'star.png'; $wpfp_options['custom_before_image'] = ''; add_option('wpfp_options', $wpfp_options, 'Favorite Posts Options'); } add_action('activate_wp-favorite-posts/wp-favorite-posts.php', 'wpfp_init'); function wpfp_config() { include('wpfp-admin.php'); } function wpfp_config_page() { if ( function_exists('add_submenu_page') ) add_options_page(__('Favorite Posts'), __('Favorite Posts'), 'manage_options', 'wp-favorite-posts', 'wpfp_config'); } add_action('admin_menu', 'wpfp_config_page'); function wpfp_update_user_meta($arr) { return update_usermeta(wpfp_get_user_id(),WPFP_META_KEY,$arr); } function wpfp_update_post_meta($post_id, $val) { $val = wpfp_get_post_meta($post_id) + $val; return add_post_meta($post_id, WPFP_META_KEY, $val, true) or update_post_meta($post_id, WPFP_META_KEY, $val); } function wpfp_delete_post_meta($post_id) { return delete_post_meta($post_id, WPFP_META_KEY); } function wpfp_list_most_favorited($limit=5) { global $wpdb; $query = "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key='".WPFP_META_KEY."' AND meta_value > 0 ORDER BY CAST(meta_value AS DECIMAL) DESC LIMIT 0, $limit"; $results = $wpdb->get_results($query); if ($results) { echo ""; } } function wpfp_widget_init() { function wpfp_widget_view($args) { extract($args); $options = wpfp_get_options(); if (isset($options['widget_limit'])) { $limit = $options['widget_limit']; } $title = empty($options['widget_title']) ? 'Most Favorited Posts' : $options['widget_title']; echo $before_widget; echo $before_title . $title . $after_title; wpfp_list_most_favorited($limit); echo $after_widget; } function wpfp_widget_control() { $options = wpfp_get_options(); if (isset($_POST["wpfp-widget-submit"])): $options['widget_title'] = strip_tags(stripslashes($_POST['wpfp-title'])); $options['widget_limit'] = strip_tags(stripslashes($_POST['wpfp-limit'])); update_option("wpfp_options", $options); endif; $title = $options['widget_title']; $limit = $options['widget_limit']; ?>

You must enable statics from favorite posts configuration page.

ID; } function wpfp_get_user_meta($user = "") { if (!empty($user)): $userdata = get_userdatabylogin($user); $user_id = $userdata->ID; return get_usermeta($user_id, WPFP_META_KEY); else: return get_usermeta(wpfp_get_user_id(), WPFP_META_KEY); endif; } function wpfp_get_post_meta($post_id) { $val = get_post_meta($post_id, WPFP_META_KEY, true); if ($val < 0) $val = 0; return $val; } function wpfp_set_cookie($post_id, $str) { $expire = time()+60*60*24*30; return setcookie("wp-favorite-posts[$post_id]", $str, $expire, "/"); } function wpfp_is_user_favlist_public($user) { $user_opts = wpfp_get_user_options($user); if ($user_opts['list_is_public']) return true; else return false; } function wpfp_get_user_options($user) { $userdata = get_userdatabylogin($user); $user_id = $userdata->ID; return get_usermeta($user_id, WPFP_USER_OPTION_KEY); } function wpfp_is_user_can_edit() { if ($_REQUEST['user']) return false; return true; } function wpfp_remove_favorite_link($post_id) { if (wpfp_is_user_can_edit()) { $wpfp_options = wpfp_get_options(); $class = 'wpfp-link remove-parent'; echo "[".$wpfp_options['rem']."]"; } } function wpfp_clear_list_link() { if (wpfp_is_user_can_edit()) { $wpfp_options = wpfp_get_options(); echo wpfp_before_link_img(); echo wpfp_loading_img(); echo "". $wpfp_options['clear'] . ""; } } function wpfp_cookie_warning() { if (!is_user_logged_in() && !isset($_GET['user']) ): echo "

".$wpfp_options['cookie_warning']."

"; endif; }