A friend created a plugin for me which allows the users to mark posts as read or unread. The code of this plugin is the following :
edit of the code !
<?php
/**
* @package Read-Unread
* @version 1.0
*/
/*
Plugin Name: Read-Unread
Plugin URI: http://www.google.fr
Description: Read-Unread plugin
Author: Moi
Version: 1.0
Author URI: http://www.google.fr
*/
global $wpdb;
$table_name = $wpdb->prefix . "users_read";
$sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
`ID_USER` int(11) NOT NULL,
`ID_POST` int(11) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
function all_css()
{
echo '
<style>
.post-read
{
cursor: pointer;
}
</style>';
}
function all_js()
{
echo '
<script>
jQuery(function($)
{
$(".post-read").click(function()
{
action = $(this).data("action");
if($(this).data("action") == "read")
{
$(this).attr("src", "http://corentinbuet.fr/wordpress/wp-content/plugins/Read-Unread/LU.png");
$(this).attr("title", "Marquer comme Non lu");
$(this).data("action", "unread");
}
else
{
$(this).attr("src", "http://corentinbuet.fr/wordpress/wp-content/plugins/Read-Unread/NON_LU.png");
$(this).attr("title", "Marquer comme Lu");
$(this).data("action", "read");
}
postID = $(this).data("id");
var data = {
"action": action,
"idPost": postID
};
$.post("/wp-admin/admin-ajax.php", data, function(response)
{
});
});
});
</script>';
}
function ajax_read() {
global $wpdb;
$idUser = intval(get_current_user_id());
$idPost = intval($_POST['idPost']);
$table = $wpdb->prefix . "users_read";
$data = array('ID_USER' => $idUser, 'ID_POST' => $idPost);
$format = array('%d','%d');
$res = $wpdb->insert( $table, $data, $format );
if($res == false)
{
return 1;
}
return 0;
die();
}
function ajax_unread() {
global $wpdb;
$idUser = intval(get_current_user_id());
$idPost = intval($_POST['idPost']);
$table = $wpdb->prefix . "users_read";
$where = array('ID_USER' => $idUser, 'ID_POST' => $idPost);
$where_format = array('%d','%d');
$res = $wpdb->delete( $table, $where, $where_format );
if($res == false)
{
return 1;
}
return 0;
die();
}
add_action( 'wp_ajax_read', 'ajax_read' );
add_action( 'wp_ajax_unread', 'ajax_unread' );
function get_img_read_unread()
{
global $wpdb;
$idUser = intval(get_current_user_id());
$idPost = intval(get_the_ID());
$msg = '';
if($idUser > 0 && $idPost > 0)
{
$read = $wpdb->get_var( "SELECT COUNT(*) FROM wp_users_read WHERE ID_POST=$idPost AND ID_USER=$idUser");
if($read == 1)
{
echo '<img class="post-read" data-action="unread" data-id="' . $idPost . '" src="http://corentinbuet.fr/wordpress/wp-content/plugins/Read-Unread/LU.png" title="Marquer comme Non lu">';
}
else
{
echo '<img class="post-read" data-action="read" data-id="' . $idPost . '" src="http://corentinbuet.fr/wordpress/wp-content/plugins/Read-Unread/NON_LU.png" title="Marquer comme Lu">';
}
}
}
function add_button($content)
{
global $wpdb;
$idUser = intval(get_current_user_id());
$idPost = intval(get_the_ID());
$msg = '';
if($idUser > 0 && $idPost > 0)
{
$read = $wpdb->get_var( "SELECT COUNT(*) FROM wp_users_read WHERE ID_POST=$idPost AND ID_USER=$idUser");
if($read == 1)
{
$msg = '<img class="post-read" data-action="unread" data-id="' . $idPost . '" src="http://corentinbuet.fr/wordpress/wp-content/plugins/Read-Unread/LU.png" title="Marquer comme Non lu">';
}
else
{
$msg = '<img class="post-read" data-action="read" data-id="' . $idPost . '" src="http://corentinbuet.fr/wordpress/wp-content/plugins/Read-Unread/NON_LU.png" title="Marquer comme Lu">';
}
}
$posttype = get_post_type( get_the_ID() );
if ($posttype == post)
{
return $content . $msg;
}
else
{
return $content;
}
}
add_filter('the_content', 'add_button');
add_action( 'wp_footer', 'all_js' );
add_action( 'wp_head', 'all_css' );
?>
I would like to mark as read a post when a post is opened by a user. So how could I do that ?
If i am not understandable enough, please let me know and i'll try to explain better.
Thank you.
Corentin
/wp-admin/) and it ignores WP errors. The MySQL table seems to contain an unused column (READ int(11)) and the AJAX functions allow users to insert an arbitrary amount of data into the database (duplicate rows allowed, no validity check of$_POST['idPost']). Static URLs are (almost) never a good idea, unless you are referencing a different domain.intval(get_current_user_id())is the same asget_current_user_id()(which always returns ints).