Skip to content

Instantly share code, notes, and snippets.

@algotrader-dotcom
Forked from hofmannsven/README.md
Created January 22, 2019 11:15
Show Gist options
  • Save algotrader-dotcom/f155df9ee5ccc41be133ff42045d944e to your computer and use it in GitHub Desktop.
Save algotrader-dotcom/f155df9ee5ccc41be133ff42045d944e to your computer and use it in GitHub Desktop.
How to create a Post Like System for WordPress.
<?php
$meta_IPS = get_post_meta($post_id, "_user_IP"); // get previously voted IP address
$ip = $_SERVER["REMOTE_ADDR"]; // Retrieve current user IP
$liked_IPS = ""; // set up array variable
if ( count( $meta_IPS ) != 0 ) { // meta exists, set up values
$liked_IPS = $meta_IPS[0];
}
if ( !is_array( $liked_IPS ) ) // make array just in case
$liked_IPS = array();
if ( in_array( $ip, $liked_IPS ) ) { // True is IP in array
return true;
}
return false;
<?php
if ( is_user_logged_in() ) { (...) }
<?php
echo getPostLikeLink( $post->ID );
?>
<?php
function frontEndUserLikes() {
$theme_object = wp_get_theme();
$themename = esc_attr( $theme_object->Name ); // the theme name
if ( is_user_logged_in() ) { // user is logged in
global $current_user;
$like_list = '';
$user_id = $current_user->ID; // current user
$user_likes = get_user_meta( $user_id, "_liked_posts");
$the_likes = ( $user_likes && count( $user_likes ) > 0 ) ? $the_likes = $user_likes[0] : $the_likes = '' ;
if ( !is_array( $the_likes ) )
$the_likes = array();
$count = count($the_likes);
if ( $count > 0 ) {
$limited_likes = array_slice( $the_likes, 0, 5 ); // this will limit the number of posts returned to 5
$like_list .= "<aside>\n";
$like_list .= "<h3>" . __( 'You Like:', $themename ) . "</h3>\n";
$like_list .= "<ul>\n";
foreach ( $limited_likes as $the_like ) {
$like_list .= "<li><a href='" . esc_url( get_permalink( $the_like ) ) . "' title='" . esc_attr( get_the_title( $the_like ) ) . "'>" . get_the_title( $the_like ) . "</a></li>\n";
}
$like_list .= "</ul>\n";
$like_list .= "</aside>\n";
}
echo $like_list;
}
}
<?php
else { // normal like button
$output .= '<span class="unliker"></span><span class="like"><i class="fa fa-heart"></i></span>';
$output .= ' <span class="count">'.$likes.'</span></a></span>&nbsp; ';
}
return $output;
<?php
function getPostLikeLink( $post_id ) {
$like_count = get_post_meta( $post_id, "_post_like_count", true ); // get post likes
$count = ( empty( $like_count ) || $like_count == "0" ) ? 'Like' : esc_attr( $like_count );
if ( AlreadyLiked( $post_id ) ) {
$class = esc_attr( ' liked' );
$title = esc_attr( 'Unlike' );
$heart = '<i class="fa fa-heart"></i>';
} else {
$class = esc_attr( '' );
$title = esc_attr( 'Like' );
$heart = '<i class="fa fa-heart-o"></i>';
}
$output = '<a href="#" class="jm-post-like'.$class.'" data-post_id="'.$post_id.'" title="'.$title.'">'.$heart.'&nbsp;'.$count.'</a>';
return $output;
}
<?php
global $current_user;
$user_likes = get_user_meta( $user->ID, "_liked_posts");
<?php
add_action( 'wp_ajax_nopriv_jm-post-like', 'jm_post_like' );
add_action( 'wp_ajax_jm-post-like', 'jm_post_like' );
<?php
$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';
} elseif ( $like_count && $like_count != "0" ) { // there are votes!
$likes = esc_attr( $like_count );
}
<?php
function jm_most_popular_today() {
global $post;
$today = date('j');
$year = date('Y');
$args = array(
'year' => $year,
'day' => $today,
'post_type' => array( 'post', 'enter-your-comma-separated-post-types-here' ),
'meta_key' => '_post_like_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 5
);
$pop_posts = new WP_Query( $args );
if ( $pop_posts->have_posts() ) {
echo "<aside>\n";
echo "<h3>Today's Most Popular Posts</h3>\n";
echo "<ul>\n";
while ( $pop_posts->have_posts() ) {
$pop_posts->the_post();
echo "<li><a href='" . get_permalink($post->ID) . "'>" . get_the_title() . "</a></li>\n";
}
echo "</ul>\n";
echo "</aside>\n";
}
wp_reset_postdata();
}
<?php
if ( isset( $_POST['jm_post_like'] ) ) {
$post_id = $_POST['post_id']; // post id
$post_like_count = get_post_meta( $post_id, "_post_like_count", true ); // post like count
if ( is_user_logged_in() ) { // user is logged in
global $current_user;
$user_id = $current_user->ID; // current user
$meta_POSTS = get_user_meta( $user_id, "_liked_posts" ); // post ids from user meta
$meta_USERS = get_post_meta( $post_id, "_user_liked" ); // user ids from post meta
$liked_POSTS = ""; // setup array variable
$liked_USERS = ""; // setup array variable
if ( count( $meta_POSTS ) != 0 ) { // meta exists, set up values
$liked_POSTS = $meta_POSTS[0];
}
if ( !is_array( $liked_POSTS ) ) // make array just in case
$liked_POSTS = array();
if ( count( $meta_USERS ) != 0 ) { // meta exists, set up values
$liked_USERS = $meta_USERS[0];
}
if ( !is_array( $liked_USERS ) ) // make array just in case
$liked_USERS = array();
$liked_POSTS['post-'.$post_id] = $post_id; // Add post id to user meta array
$liked_USERS['user-'.$user_id] = $user_id; // add user id to post meta array
$user_likes = count( $liked_POSTS ); // count user likes
}
}
<?php
query_posts('meta_key=_post_like_count&orderby=meta_value_num&order=DESC&posts_per_page=5');
?>
<?php
else {
echo '<p>You don\'t like anything yet.</p>';
}
<?php
$nonce = $_POST['nonce'];
if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
die ( 'Nope!' );
<?php
if ( !AlreadyLiked( $post_id ) ) { // like the post
update_post_meta( $post_id, "_user_liked", $liked_USERS ); // Add user ID to post meta
update_post_meta( $post_id, "_post_like_count", ++$post_like_count ); // +1 count post meta
update_user_meta( $user_id, "_liked_posts", $liked_POSTS ); // Add post ID to user meta
update_user_meta( $user_id, "_user_like_count", $user_likes ); // +1 count user meta
echo $post_like_count; // update count on front end
}
<?php
function jm_like_shortcode() {
return getPostLikeLink( get_the_ID() );
}
add_shortcode('jmliker', 'jm_like_shortcode');
<?php
$output = '<span class="jm-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"><i class="fa fa-heart"></i></span>';
$output .= ' <span class="count alreadyliked">'.$likes.'</span></a></span>&nbsp; ';
}
<?php
else { // unlike the post
$pid_key = array_search( $post_id, $liked_POSTS ); // find the key
$uid_key = array_search( $user_id, $liked_USERS ); // find the key
unset( $liked_POSTS[$pid_key] ); // remove from array
unset( $liked_USERS[$uid_key] ); // remove from array
$user_likes = count( $liked_POSTS ); // recount user likes
update_post_meta( $post_id, "_user_liked", $liked_USERS ); // Remove user ID from post meta
update_post_meta($post_id, "_post_like_count", --$post_like_count ); // -1 count post meta
update_user_meta( $user_id, "_liked_posts", $liked_POSTS ); // Remove post ID from user meta
update_user_meta( $user_id, "_user_like_count", $user_likes ); // -1 count user meta
echo "already".$post_like_count; // update count on front end
}
<?php
if ( !empty( $user_likes ) && count( $user_likes ) > 0 ) {
$the_likes = $user_likes[0];
} else {
$the_likes = '';
}
if ( !is_array( $the_likes ) )
$the_likes = array();
$count = count($the_likes); $i=0;
if ( $count > 0 ) {
$like_list = '';
echo '<p>';
foreach ( $the_likes as $the_like ) {
$i++;
$like_list .= '<a href="' . esc_url( get_permalink( $the_like ) ) . '" title="' . esc_attr( get_the_title( $the_like ) ) . '">' . get_the_title( $the_like ) . '</a>';
if ($count != $i) $like_list .= ' &middot; ';
else $like_list .= '</p>';
}
echo $like_list;
}
<?php
global $current_user;
$user_id = $current_user->ID; // current user
$meta_USERS = get_post_meta( $post_id, "_user_liked" ); // user ids from post meta
$liked_USERS = ""; // set up array variable
if ( count( $meta_USERS ) != 0 ) { // meta exists, set up values
$liked_USERS = $meta_USERS[0];
}
if( !is_array( $liked_USERS ) ) // make array just in case
$liked_USERS = array();
if ( in_array( $user_id, $liked_USERS ) ) { // True if User ID in array
return true;
}
return false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment