javascript onload calling a function of a plugin

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="https://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="https://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="https://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="https://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

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

javascript onload calling a function of a plugin

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.