调用插件的函数的JavaScript onLoad

时间:2014-07-31 作者:boulgour

一位朋友为我创建了一个插件,允许用户将帖子标记为已读或未读。此插件的代码如下:

编辑代码!

<?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\' );


?>
当用户打开帖子时,我想将其标记为已读帖子。那我该怎么做呢?

如果我不能理解,请让我知道,我会尽量解释得更好。

非常感谢。

科朗坦

1 个回复
SO网友:Tobias

我没有测试这个,但类似的东西应该可以工作。编辑函数all_js 在插件文件中添加以下行:

$.post("/wp-admin/admin-ajax.php", {
  idPost: $(".post-read").data("id"),
  action: "read"
}, function(response) {
  // Handle errors etc.
});
您还需要确保在发出AJAX请求时按钮数据发生更改。

下面是一个改进的脚本(虽然还远远不够完美,但还是一个改进):

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();
        });
    });
});

结束

相关推荐

在WordPress导航菜单之前和之后插入一个javascript横幅代码

我正在尝试开发一个插件,在wordpress导航菜单前后插入javascript横幅代码,但我找不到任何解决此问题的方法。有人知道吗?我也不能附加到Div ID,因为每个wordpress主题都使用不同的Div ID和类作为导航菜单。谢谢编辑:横幅代码是adsense javascript代码。Wordpress有wp\\u nav\\u菜单功能,我想在wp\\u nav\\u菜单之前和之后注入javascript。