Skip to content

Instantly share code, notes, and snippets.

@hofmannsven
Last active May 15, 2019 06:41
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save hofmannsven/6421354 to your computer and use it in GitHub Desktop.
Save hofmannsven/6421354 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
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;
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
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
$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;
@SHABU89
Copy link

SHABU89 commented Jul 23, 2018

How to Implement this code with a wordpress theme ?

@hofmannsven
Copy link
Author

You can find the full documentation here:
https://github.com/JonMasterson/WordPress-Post-Like-System

@SHABU89
Copy link

SHABU89 commented Oct 20, 2018

Is there any possibility to keep a remove button from in the list of liked posts (current-user-likes.php) In this is there any option to keep remove button for each liked post to remove individually.

@SHABU89
Copy link

SHABU89 commented Oct 21, 2018

Removing liked posts from page template not working. But added same code how it was for posts page.

<?php
/*
* Template Name: Liked Posts
*/
get_header();
$current_user = get_current_user_id();
?>
	<table class="form-table">
		<tr>
			<th><label for="user_likes"><?php _e( 'You Like:', 'YourThemeTextDomain' ); ?></label></th>
			<td>
			<?php
			$types = get_post_types( array( 'public' => true ) );
			$args = array(
			  'numberposts' => -1,
			  'post_type' => $types,
			  'meta_query' => array (
				array (
				  'key' => '_user_liked',
				  'value' => $current_user,
				  'compare' => 'LIKE'
				)
			  ) );		
			$sep = '';
			$like_query = new WP_Query( $args );
			if ( $like_query->have_posts() ) : ?>
			<p>
			<?php 
            
 while ( $like_query->have_posts() ) : $like_query->the_post(); 
 echo $sep; 
 $post_id = get_the_ID();
 $nonce = wp_create_nonce( 'simple-likes-nonce' );
 $is_comment = '0';
?>
 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
 <?php the_title(); ?></a> 
 <?php echo  $post_id; ?>
 <a href="<?php echo admin_url( 'admin-ajax.php?action=process_simple_like' . '&post_id=' . $post_id . '&nonce=' . $nonce . '&is_comment=' . $is_comment . '&disabled=true') ?>">Remove</a>
 <?php echo do_shortcode('[jmliker]'); ?>
			<?php
			$sep = ' &middot; ';
			endwhile; 
			?>
			</p>
			<?php else : ?>
			<p><?php _e( 'You do not like anything yet.', 'YourThemeTextDomain' ); ?></p>
			<?php 
			endif; 
			wp_reset_postdata(); 
			?>
			</td>
		</tr>
	</table>
<?php get_footer(); ?>

@SHABU89
Copy link

SHABU89 commented Nov 15, 2018

Getting following error while integrating.

Fatal error: Uncaught Error: Call to undefined function AlreadyLiked() in

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