Create personalized user experiences with the I Recommend This plugin
The "I Recommend This" plugin provides a simple and elegant way for visitors to recommend your WordPress posts. But did you know you can go beyond the basic implementation to create rich, personalized experiences for your users?
In this guide, we'll explore advanced techniques for leveraging user recommendation data to enhance your WordPress site. These customizations will help you create more engaging user experiences and potentially increase user interaction with your content.
Understanding User Recommendation Status
At the core of these customizations is the ability to check whether a user has recommended a specific post. The plugin stores this information in browser cookies, making it accessible through PHP.
Here's a simple helper function to check if a user has recommended a post:
/**
* Check if the current user has already recommended a post
*
* @param int $post_id The post ID to check
* @return bool True if user has recommended the post
*/
function has_user_recommended( $post_id ) {
if ( ! is_numeric( $post_id ) ) {
return false;
}
$cookie_name = 'irecommendthis_' . $post_id;
return isset( $_COOKIE[ $cookie_name ] );
}
With this function in place, you can create conditional experiences based on a user's recommendation activity.
1. Custom Thank You Messages
One of the simplest yet effective customizations is to show a personalized thank you message to users who have recommended your content.
<?php
// After your post content
if ( has_user_recommended( get_the_ID() ) ) : ?>
<div class="recommendation-thanks">
<h4>Thank you for your recommendation!</h4>
<p>We appreciate your support and hope you'll share this article with others who might find it helpful.</p>
</div>
<?php else : ?>
<div class="recommendation-prompt">
<p>If you found this article helpful, please consider recommending it!</p>
<?php if ( function_exists( 'irecommendthis' ) ) irecommendthis(); ?>
</div>
<?php endif; ?>
This creates a more engaging experience by acknowledging the user's action and encouraging further engagement.
2. Exclusive Content for Supporters
Want to reward users who recommend your posts? Consider offering exclusive content or bonus materials only to those who have recommended the article.
<?php
// Your regular post content here
// Bonus content for supporters
if ( has_user_recommended( get_the_ID() ) ) : ?>
<div class="exclusive-content">
<h3>Bonus Content for Supporters</h3>
<div class="bonus-box">
<p>Thank you for recommending this article! Here's some exclusive content just for you:</p>
<?php
// This could be a downloadable resource, extra tips, etc.
echo do_shortcode('[download_link file="exclusive-resource.pdf" text="Download Your Free Resource"]');
?>
<div class="extra-tips">
<h4>Extra Tips</h4>
<ul>
<li>Advanced tip #1: Lorem ipsum dolor sit amet</li>
<li>Advanced tip #2: Consectetur adipiscing elit</li>
<li>Advanced tip #3: Sed do eiusmod tempor incididunt</li>
</ul>
</div>
</div>
</div>
<?php else : ?>
<div class="locked-content-teaser">
<p><strong>Want access to exclusive bonus content?</strong> Recommend this article to unlock additional tips and resources!</p>
<?php if ( function_exists( 'irecommendthis' ) ) irecommendthis(); ?>
</div>
<?php endif; ?>
This approach can significantly increase user engagement by providing a tangible reward for the simple action of recommending your content.
3. Personalized Reading Lists
You can create a personalized reading list for users based on the posts they've recommended. This is a great addition to a user dashboard or sidebar.
<?php
/**
* Retrieve posts the current user has recommended
*
* @return array Array of post IDs
*/
function get_user_recommended_posts() {
$recommended_posts = array();
// Check cookies for recommendations
foreach ($_COOKIE as $name => $value) {
if (strpos($name, 'irecommendthis_') === 0) {
$post_id = str_replace('irecommendthis_', '', $name);
$recommended_posts[] = intval($post_id);
}
}
return $recommended_posts;
}
// Sidebar or dashboard widget implementation
function display_user_recommendations_widget() {
$recommended_posts = get_user_recommended_posts();
if (empty($recommended_posts)) {
return '<div class="user-recommendations empty">
<h3>Your Recommendations</h3>
<p>You haven\'t recommended any posts yet. Start recommending posts you enjoy to build your list!</p>
</div>';
}
// Get up to 5 most recent recommendations
$recent_recommendations = array_slice($recommended_posts, 0, 5);
$output = '<div class="user-recommendations">';
$output .= '<h3>Your Recommended Posts</h3>';
$output .= '<ul class="recommendation-list">';
foreach ($recent_recommendations as $post_id) {
$post_title = get_the_title($post_id);
$post_url = get_permalink($post_id);
$post_date = get_the_date('', $post_id);
$output .= '<li class="recommendation-item">';
$output .= '<a href="' . esc_url($post_url) . '">' . esc_html($post_title) . '</a>';
$output .= '<span class="recommendation-date">' . esc_html($post_date) . '</span>';
$output .= '</li>';
}
$output .= '</ul>';
if (count($recommended_posts) > 5) {
$output .= '<p class="view-all"><a href="/my-recommendations/">View all ' . count($recommended_posts) . ' recommendations</a></p>';
}
$output .= '</div>';
return $output;
}
// Usage
echo display_user_recommendations_widget();
?>
You can extend this further by creating a dedicated page template that shows all recommendations with filtering options.
4. Social Proof and Promotion
Encourage users who recommend your content to share it on social media by showing a custom share prompt:
<?php if ( has_user_recommended( get_the_ID() ) ) : ?>
<div class="social-share-prompt">
<h4>Thanks for recommending!</h4>
<p>Would you like to share this article with your friends?</p>
<div class="share-buttons">
<?php
$post_url = urlencode(get_permalink());
$post_title = urlencode(get_the_title());
?>
<a href="https://twitter.com/intent/tweet?url=<?php echo $post_url; ?>&text=<?php echo $post_title; ?>" target="_blank" class="twitter-share">
<span class="icon"></span> Share on Twitter
</a>
<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo $post_url; ?>" target="_blank" class="facebook-share">
<span class="icon"></span> Share on Facebook
</a>
<a href="https://www.linkedin.com/sharing/share-offsite/?url=<?php echo $post_url; ?>" target="_blank" class="linkedin-share">
<span class="icon"></span> Share on LinkedIn
</a>
</div>
</div>
<?php endif; ?>
5. Gamification Elements
Take user engagement to the next level by adding gamification elements based on recommendation activity:
<?php
/**
* Get user recommendation stats
*/
function get_user_recommendation_stats() {
$recommended_posts = get_user_recommended_posts();
$count = count($recommended_posts);
$categories = array();
foreach ($recommended_posts as $post_id) {
$post_categories = get_the_category($post_id);
foreach ($post_categories as $category) {
if (!isset($categories[$category->term_id])) {
$categories[$category->term_id] = 0;
}
$categories[$category->term_id]++;
}
}
// Find favorite category
$favorite_category_id = 0;
$favorite_category_count = 0;
foreach ($categories as $cat_id => $cat_count) {
if ($cat_count > $favorite_category_count) {
$favorite_category_id = $cat_id;
$favorite_category_count = $cat_count;
}
}
$favorite_category_name = ($favorite_category_id > 0) ? get_cat_name($favorite_category_id) : '';
return array(
'total_count' => $count,
'favorite_category' => $favorite_category_name,
'favorite_category_count' => $favorite_category_count
);
}
// Display user badges based on recommendation activity
function display_user_badges() {
$stats = get_user_recommendation_stats();
$badges = array();
// Total recommendations badges
if ($stats['total_count'] >= 1) {
$badges[] = array(
'name' => 'First Recommendation',
'description' => 'You made your first recommendation!',
'icon' => ''
);
}
if ($stats['total_count'] >= 5) {
$badges[] = array(
'name' => 'Regular Supporter',
'description' => 'You\'ve recommended 5 or more posts!',
'icon' => ''
);
}
if ($stats['total_count'] >= 10) {
$badges[] = array(
'name' => 'Enthusiastic Fan',
'description' => 'You\'ve recommended 10 or more posts!',
'icon' => ''
);
}
// Category specialist
if ($stats['favorite_category_count'] >= 3 && !empty($stats['favorite_category'])) {
$badges[] = array(
'name' => $stats['favorite_category'] . ' Specialist',
'description' => 'You\'ve recommended 3 or more posts in the ' . $stats['favorite_category'] . ' category!',
'icon' => ''
);
}
// Display badges
if (empty($badges)) {
return '<div class="user-badges empty">
<h3>Your Badges</h3>
<p>Start recommending posts to earn badges!</p>
</div>';
}
$output = '<div class="user-badges">';
$output .= '<h3>Your Badges</h3>';
$output .= '<ul class="badges-list">';
foreach ($badges as $badge) {
$output .= '<li class="badge-item">';
$output .= '<span class="badge-icon">' . $badge['icon'] . '</span>';
$output .= '<div class="badge-details">';
$output .= '<h4>' . esc_html($badge['name']) . '</h4>';
$output .= '<p>' . esc_html($badge['description']) . '</p>';
$output .= '</div>';
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
return $output;
}
// Usage
echo display_user_badges();
?>
6. Smart Content Recommendations
Based on a user's recommendation history, you can suggest similar content they might enjoy:
<?php
/**
* Get content recommendations based on user's recommendation history
*/
function get_smart_content_recommendations() {
$recommended_posts = get_user_recommended_posts();
if (empty($recommended_posts)) {
// Fall back to popular posts if no recommendations
return get_popular_posts(3);
}
// Get categories from recommended posts
$categories = array();
foreach ($recommended_posts as $post_id) {
$post_cats = wp_get_post_categories($post_id);
foreach ($post_cats as $cat) {
if (!isset($categories[$cat])) {
$categories[$cat] = 0;
}
$categories[$cat]++;
}
}
// Sort by frequency
arsort($categories);
// Get top 3 categories
$top_categories = array_slice(array_keys($categories), 0, 3);
// Get posts from these categories that user hasn't read yet
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'category__in' => $top_categories,
'post__not_in' => $recommended_posts,
'orderby' => 'date',
'order' => 'DESC'
);
$posts_query = new WP_Query($args);
if (!$posts_query->have_posts()) {
return get_popular_posts(3);
}
$output = '<div class="smart-recommendations">';
$output .= '<h3>Recommended For You</h3>';
$output .= '<div class="recommendations-grid">';
while ($posts_query->have_posts()) {
$posts_query->the_post();
$output .= '<div class="recommendation-card">';
if (has_post_thumbnail()) {
$output .= '<div class="recommendation-image">';
$output .= get_the_post_thumbnail(null, 'medium');
$output .= '</div>';
}
$output .= '<div class="recommendation-content">';
$output .= '<h4><a href="' . get_permalink() . '">' . get_the_title() . '</a></h4>';
$output .= '<p>' . get_the_excerpt() . '</p>';
$output .= '</div>';
$output .= '</div>';
}
wp_reset_postdata();
$output .= '</div>';
$output .= '</div>';
return $output;
}
/**
* Fallback function to get popular posts
*/
function get_popular_posts($count = 3) {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $count,
'meta_key' => '_recommended',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$posts_query = new WP_Query($args);
if (!$posts_query->have_posts()) {
return '';
}
$output = '<div class="popular-recommendations">';
$output .= '<h3>Popular Posts</h3>';
$output .= '<div class="recommendations-grid">';
while ($posts_query->have_posts()) {
$posts_query->the_post();
$output .= '<div class="recommendation-card">';
if (has_post_thumbnail()) {
$output .= '<div class="recommendation-image">';
$output .= get_the_post_thumbnail(null, 'medium');
$output .= '</div>';
}
$output .= '<div class="recommendation-content">';
$output .= '<h4><a href="' . get_permalink() . '">' . get_the_title() . '</a></h4>';
$output .= '<p>' . get_the_excerpt() . '</p>';
$output .= '</div>';
$output .= '</div>';
}
wp_reset_postdata();
$output .= '</div>';
$output .= '</div>';
return $output;
}
// Usage
echo get_smart_content_recommendations();
?>
7. Custom Styling Based on Recommendation Status
You can even add custom styling to your theme based on whether a user has recommended the current post:
<?php
// Add to your theme's header.php or via wp_head action
add_action('wp_head', 'add_recommendation_custom_styles');
function add_recommendation_custom_styles() {
if (is_single() && has_user_recommended(get_the_ID())) {
?>
<style type="text/css">
/* Custom styles for users who have recommended this post */
body.single-post {
/* Subtle indicator at the top of the page */
border-top: 4px solid #5cb85c;
}
/* Style the recommendation button differently */
.irecommendthis.active {
background-color: #5cb85c !important;
color: white !important;
padding: 5px 10px !important;
border-radius: 4px !important;
transition: all 0.3s ease !important;
}
/* Add a special background to the post title */
.single-post .entry-title {
position: relative;
}
.single-post .entry-title:after {
content: "❤️ Recommended";
position: absolute;
top: -20px;
right: 0;
font-size: 14px;
color: #5cb85c;
font-weight: normal;
}
</style>
<?php
}
}
?>
Implementation Tips
To implement these customizations effectively:
- Add the helper functions to your theme's functions.php file or create a small custom plugin.
- Test thoroughly with different user scenarios (new visitors, returning visitors who haven't recommended, and those who have).
- Consider performance implications when querying for multiple posts or adding complex functionality.
- Use caching for more complex functions like the smart recommendations.
- Make your customizations mobile-friendly to ensure a good experience on all devices.
Conclusion
The "I Recommend This" plugin provides more than just a simple like button - it's a foundation for creating personalized user experiences. By leveraging the recommendation data stored in cookies, you can create sophisticated customizations that enhance engagement, reward loyal readers, and create a more dynamic website.
These customizations can particularly benefit blogs, magazines, and content-driven websites where user engagement is a priority. By making readers feel recognized and rewarded for their engagement, you create a stronger connection with your audience that keeps them coming back for more.
Give these customizations a try, and see how they can transform your WordPress site from a static collection of posts into an interactive, personalized experience for your visitors!