Created
March 16, 2015 08:44
-
-
Save vishalbasnet23/f60fcffdc42d07932736 to your computer and use it in GitHub Desktop.
Simple Heart Extenstion for WordPress Posts
This file contains hidden or 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 | |
/** | |
* Get Ip function | |
*/ | |
function get_current_user_ip() { | |
if (!empty($_SERVER['HTTP_CLIENT_IP'])) : | |
$ip = $_SERVER['HTTP_CLIENT_IP']; | |
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) : | |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
else : | |
$ip = $_SERVER['REMOTE_ADDR']; | |
endif; | |
return $ip; | |
} | |
// ajax Callback | |
add_action('wp_ajax_heart_it', 'heart_it', 0); | |
add_action('wp_ajax_nopriv_heart_it', 'heart_it'); | |
function heart_it() { | |
$user_ip_address = get_current_user_ip(); | |
$user_unique_name = str_replace('.', '_', $user_ip_address); | |
$post_id = $_POST['post_id']; | |
$posts_liked_by_current_user = get_option('liked_by_'.$user_unique_name); | |
$post_like_count = get_post_meta($post_id, 'like_count', true); | |
if( empty($posts_liked_by_current_user) ) : | |
add_option('liked_by_'.$user_unique_name, array($post_id), false); | |
$post_like_count++; | |
update_post_meta($post_id, 'like_count', $post_like_count); | |
$message = 'You have liked this post'; | |
else : | |
if( in_array($post_id, $posts_liked_by_current_user) ) : | |
$message = 'liked'; | |
else : | |
$post_like_count++; | |
array_push($posts_liked_by_current_user, $post_id); | |
update_option('liked_by_'.$user_unique_name, $posts_liked_by_current_user); | |
update_post_meta($post_id, 'like_count', $post_like_count); | |
$message = 'You have liked this post'; | |
endif; //in_array | |
endif; //is liked posts empty | |
$message_return = array('message'=>$message, 'like_count'=>$post_like_count); | |
$message_json = json_encode($message_return); | |
echo $message_json; | |
die; | |
} |
This file contains hidden or 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 | |
while(have_posts()): the_post(); | |
the_title(); | |
echo '<div class="like" data-id="'.$post->ID.'" user-id="'.get_current_user_id().'">Like</div>'; | |
endwhile; | |
<script> | |
jQuery('.like').on('click', function() { | |
var postID = jQuery(this).attr('data-id'); | |
var clickedThis = jQuery(this).parent().children('span.like').children().children('span'); | |
jQuery.ajax({ | |
type: 'POST', | |
url: ajaxUrl, | |
data: { | |
action: "heart_it", | |
post_id: postID, | |
}, | |
success: function(result) { | |
var responseJSON = jQuery.parseJSON(result); | |
console.log(responseJSON.message); | |
if( responseJSON.message != 'liked' ) { | |
clickedThis.text(responseJSON.like_count); | |
} | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment