wordpress登录插件_构建一个WordPress用户登录计数器插件

wordpress登录插件

WordPress is arguably the most popular content management system on the web.

WordPress可以说是网络上最流行的内容管理系统。

According to Forbes, over 60 million websites globally are powered by it. Numbers like this show that WordPress is no doubt a leading contender when it comes to Content Management Systems (CMS).

据《福布斯》报道,全球有超过6000万个网站受其支持。 像这样的数字表明,WordPress在内容管理系统(CMS)方面无疑是领先的竞争者。

A major attraction of WordPress is its large pool of plugins. Want to build an eCommerce store? There’s WooCommerce. How about a job portal? There’s WP Job Manager.

WordPress的主要吸引力在于其庞大的插件库。 要建立电子商务商店吗? 有WooCommerce 。 工作门户怎么样? 有WP Job Manager

In this tutorial, we will learn how to build a plugin that counts the number of times users log in to a WordPress powered site with the login stats displayed in a custom column in user list page.

在本教程中,我们将学习如何构建一个插件,该插件使用用户列表页面的自定义列中显示的登录统计信息来计算用户登录到WordPress支持的站点的次数。

User listing with login count

插件开发 (Plugin Development)

The majority of files in a WordPress plugin consist of PHP files, located in /wp-content/plugins/ directory. In our example, the file will be called wordpress-login-count.php. I assume you’re comfortable with connecting to your server using FTP/SFTP/SCP or SSH.

WordPress插件中的大多数文件由位于/wp-content/plugins/目录中PHP文件组成。 在我们的示例中,该文件将称为wordpress-login-count.php 。 我认为您可以使用FTP / SFTP / SCP或SSH连接到服务器。

If you want to follow along, create the plugin PHP file wordpress-login-count.php. The complete plugin will be available for download at the end of this tutorial.

如果要继续,请创建插件PHP文件wordpress-login-count.php 。 完整的插件将在本教程末尾提供下载。

First off, include the plugin header. Without the header, WordPress will not recognize the plugin.

首先,包括插件头。 没有标题,WordPress将无法识别该插件。

<?php

/*
Plugin Name: WordPress User Login Counter
Plugin URI: http://sitepoint.com
Description: Count the number of times users log in to their WordPress account.
Version: 1.0
Author: Agbonghama Collins
Author URI: http://w3guy.com
License: GPL2
*/

We then add a PHP namespace and create the plugin class as follows.

然后,我们添加一个PHP名称空间并创建插件类,如下所示。

namespace Sitepoint\WordPressPlugin;

class Login_Counter {
// ...

All action and filter hooks required by the plugin will go into the init() method.

插件所需的所有操作和过滤器挂钩都将放入init()方法中。

public function init() {
        add_action( 'wp_login', array( $this, 'count_user_login' ), 10, 2 );

        add_filter( 'manage_users_columns', array( $this, 'add_stats_columns' ) );

        add_action( 'manage_users_custom_column', array( $this, 'fill_stats_columns' ), 10, 3 );
    }

The wp_login action hook is triggered by WordPress when a user logs in, thus this is the appropriate hook for us to use to count a user log in.

当用户登录时, wp_login操作挂钩由WordPress触发,因此这是我们用来计算用户登录次数的合适挂钩。

The function count_user_login() below does the counting.

下面的函数count_user_login()进行计数。

/**
     * Save user login count to Database.
     *
     * @param string $user_login username
     * @param object $user WP_User object
     */
    public function count_user_login( $user_login, $user ) {

        if ( ! empty( get_user_meta( $user->ID, 'sp_login_count', true ) ) ) {
            $login_count = get_user_meta( $user->ID, 'sp_login_count', true );
            update_user_meta( $user->ID, 'sp_login_count', ( (int) $login_count + 1 ) );
        } else {
            update_user_meta( $user->ID, 'sp_login_count', 1 );
        }
    }

Code explanation: first we check if a user has an empty sp_login_count meta field. If false, we get the previously saved login count and increment it by one (1) and if true, it therefore means the user is logging in for the first time. As a result, the value 1 will be saved against the user meta field.

代码说明:首先,我们检查用户是否有一个空的sp_login_count元字段。 如果为false,则将获得先前保存的登录计数,并将其递增一(1);如果为true,则意味着用户是首次登录。 结果,将在用户元字段中保存值1

The manage_users_custom_column filter for adding an additional column to the WordPress user list page is used to add a Login Count column that will output the number of times a user has logged in (see screenshot above).

用于在WordPress用户列表页面中添加其他列的manage_users_custom_column过滤器用于添加“ 登录计数”列,该列将输出用户登录的次数(请参见上面的屏幕截图)。

The function fill_stats_columns() hooked into manage_users_custom_column adds the new column.

挂钩到manage_users_custom_column的函数fill_stats_columns()添加了新列。

/**
     * Fill the stat column with values.
     *
     * @param string $empty
     * @param string $column_name
     * @param int $user_id
     *
     * @return string|void
     */
    public function fill_stats_columns( $empty, $column_name, $user_id ) {

        if ( 'login_stat' == $column_name ) {
            if ( get_user_meta( $user_id, 'sp_login_count', true ) !== '' ) {
                $login_count = get_user_meta( $user_id, 'sp_login_count', true );

                return "<strong>$login_count</strong>";
            } else {
                return __( 'No record found.' );
            }
        }

        return $empty;
    }

Code explanation: The first if condition ensures we are actually in the login_stat column. The next if condition checks if a login count exists for the user. If true, it returns the login count, or it returns the text No record found.

代码说明:第一个if条件确保我们实际上在login_stat列中。 下一个if条件检查用户是否存在登录计数。 如果为true,则返回登录计数,或者返回文本No record found

The get_instance() method creates a singleton instance of the class and then calls the init() method to register the various action and filter hooks.

get_instance()方法创建该类的单例实例,然后调用init()方法来注册各种操作和过滤器挂钩。

Finally, we’ll make a call to the get_instance() method to put the PHP class to work.

最后,我们将调用get_instance()方法以使PHP类正常工作。

Login_Counter::get_instance();

Voila! We are done coding our login counter plugin.

瞧! 我们已经完成了登录计数器插件的编码。

结语 (Wrap Up)

To further understand how the plugin was built and to implement it in your WordPress powered website, download it from GitHub.

要进一步了解该插件的构建方式以及如何在您的WordPress网站上实现该插件,请从GitHub下载。

I hope this will be of help to you in learning how to develop plugins for WordPress.

希望对您学习如何为WordPress开发插件有帮助。

Let us know your thoughts in the comments.

在评论中让我们知道您的想法。

翻译自: https://www.sitepoint.com/building-a-wordpress-user-login-counter-plugin/

wordpress登录插件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值