Skip to content

Instantly share code, notes, and snippets.

@JonMasterson
Created November 5, 2013 17:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonMasterson/7323000 to your computer and use it in GitHub Desktop.
Save JonMasterson/7323000 to your computer and use it in GitHub Desktop.
Swap Font Awesome Icons based on the number of likes post receives. Can be used with my WordPress Like System.
<?php
/**
* Swap Function (5) with the following
* (5)(b) Front end button
*/
function getPostLikeLink( $post_id ) {
$theme_object = wp_get_theme();
$themename = esc_attr( $theme_object->Name ); // the theme name
$like_count = get_post_meta( $post_id, "_post_like_count", true ); // get post likes
if ( ( !$like_count ) || ( $like_count && $like_count == 0 ) ) { // no votes, set up empty variable
$likes = 'Like';
$icon = '<i class="fa fa-heart"></i>'; // no likes, icon is heart
} elseif ( $like_count && $like_count != 0 ) {
$likes = esc_attr( $like_count );
if ( $like_count <= 19 ) {
$icon = '<i class="fa fa-star-o"></i>'; // 1-19 likes, icon is empty star
} elseif ( $like_count <= 29 ) {
$icon = '<i class="fa fa-star-half-empty"></i>'; // 20-29 likes, icon is half-full star
} elseif ( $like_count <= 49 ) {
$icon = '<i class="fa fa-star"></i>'; // 30-49 likes, icon is full star
} elseif ( $like_count > 49 ) {
$icon = '<i class="fa fa-rocket"></i>'; // 50 or more likes, icon is rocket ship
}
}
$output = '<span class="post-like">';
$output .= '<a href="#" data-post_id="'.$post_id.'">';
if ( AlreadyLiked( $post_id ) ) { // already liked, set up unlike addon
$output .= '<span class="unliker"><i class="fa fa-times-circle"></i></span><span class="like prevliked">'.$icon.'</i></span>';
$output .= ' <span class="count alreadyliked">'.$likes.'</span></a></span>&nbsp; ';
} else { // normal like button
$output .= '<span class="unliker"></span><span class="like">'.$icon.'</span>';
$output .= ' <span class="count">'.$likes.'</span></a></span>&nbsp; ';
}
return $output;
}
jQuery(document).ready(function() {
jQuery(".post-like a").click(function(){
heart = jQuery(this);
post_id = heart.data("post_id");
jQuery.ajax({
type: "post",
url: ajax_var.url,
data: "action=post-like&nonce="+ajax_var.nonce+"&post_like=&post_id="+post_id,
success: function(count){
if( count.indexOf( "already" ) !== -1 )
{
var lecount = count.replace("already","");
if (lecount==0)
{
var lecount = "Like";
var icon = "<i class='fa fa-heart'></i>";
}
else if (lecount<=19)
{
var icon = "<i class='fa fa-star-o'></i>";
}
else if (lecount<=29)
{
var icon = "<i class='fa fa-star-half-empty'></i>";
}
else if (lecount<=49)
{
var icon = "<i class='fa fa-star'></i>";
}
else if (lecount>49)
{
var icon = "<i class='fa fa-rocket'></i>";
}
heart.children(".like").removeClass("pastliked").addClass("disliked").html(icon);
heart.children(".unliker").text("");
heart.children(".count").removeClass("liked").addClass("disliked").text(lecount);
}
else
{
if (count==0)
{
var icon = "<i class='fa fa-heart'></i>";
}
else if (count<=19)
{
var icon = "<i class='fa fa-star-o'></i>";
}
else if (count<=29)
{
var icon = "<i class='fa fa-star-half-empty'></i>";
}
else if (count<=49)
{
var icon = "<i class='fa fa-star'></i>";
}
else if (count>49)
{
var icon = "<i class='fa fa-rocket'></i>";
}
heart.children(".like").addClass("pastliked").removeClass("disliked").html(icon);
heart.children(".unliker").html("<i class='fa fa-times-circle'></i>");
heart.children(".count").addClass("liked").removeClass("disliked").text(count);
}
}
});
return false;
})
})
@pudan-k
Copy link

pudan-k commented Jul 7, 2014

Thanks for the great plugin. You rockzzz...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment