I did not test this, but something like this should work. Edit the function all_js
in your plugin file and add these lines:
$.post("/wp-admin/admin-ajax.php", {
idPost: $(".post-read").data("id"),
action: "read"
}, function(response) {
// Handle errors etc.
});
You also need to ensure that the button data changes when the AJAX request is made.
Here is an improved script (still far from perfect, but an improvement):
jQuery(function($)
{
var button = $(".post-read"),
postId = button.data("id");
// Updates button attributes to match its data-action attribute
function updateButton() {
if(button.data("action") == "unread")
{
button.attr("src", "http://corentinbuet.fr/wordpress/wp-content/plugins/Read-Unread/LU.png");
button.attr("title", "Marquer comme Non lu");
}
else
{
button.attr("src", "http://corentinbuet.fr/wordpress/wp-content/plugins/Read-Unread/NON_LU.png");
button.attr("title", "Marquer comme Lu");
}
}
// Mark post as read
$.post("/wp-admin/admin-ajax.php", {
idPost: postId,
action: "read"
}, function(response) {
// Handle errors etc.
button.data('action', 'unread');
updateButton();
});
$(".post-read").click(function()
{
// Fix: Declare variable
var action = button.data("action");
var data = {
"action": action,
"idPost": postId
};
$.post("/wp-admin/admin-ajax.php", data, function(response)
{
// Check for errors etc
// Update button
button.data("action", (action == "read") ? "unread" : "read");
updateButton();
});
});
});