Created
November 5, 2013 17:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> '; | |
} else { // normal like button | |
$output .= '<span class="unliker"></span><span class="like">'.$icon.'</span>'; | |
$output .= ' <span class="count">'.$likes.'</span></a></span> '; | |
} | |
return $output; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the great plugin. You rockzzz...