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插件中的大多数文件由位于目录中PHP文件组成。 在我们的示例中,该文件将称为 。 我认为您可以使用FTP / SFTP / SCP或SSH连接到服务器。
If you want to follow along, create the plugin PHP file . The complete plugin will be available for download at the end of this tutorial.
如果要继续,请创建插件PHP文件 。 完整的插件将在本教程末尾提供下载。
First off, include the plugin header. Without the header, WordPress will not recognize the plugin.
首先,包括插件头。 没有标题,WordPress将无法识别该插件。
We then add a PHP namespace and create the plugin class as follows.
然后,我们添加一个PHP名称空间并创建插件类,如下所示。
All action and filter hooks required by the plugin will go into the method.
插件所需的所有操作和过滤器挂钩都将放入方法中。
The 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.
当用户登录时, 操作挂钩由WordPress触发,因此这是我们用来计算用户登录次数的合适挂钩。
The function below does the counting.
下面的函数进行计数。
Code explanation: first we check if a user has an empty 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 will be saved against the user meta field.
代码说明:首先,我们检查用户是否有一个空的元字段。 如果为false,则将获得先前保存的登录计数,并将其递增一(1);如果为true,则意味着用户是首次登录。 结果,将在用户元字段中保存值 。
The 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用户列表页面中添加其他列的过滤器用于添加“ 登录计数”列,该列将输出用户登录的次数(请参见上面的屏幕截图)。
The function hooked into adds the new column.
挂钩到的函数添加了新列。
Code explanation: The first condition ensures we are actually in the column. The next condition checks if a login count exists for the user. If true, it returns the login count, or it returns the text .
代码说明:第一个条件确保我们实际上在列中。 下一个条件检查用户是否存在登录计数。 如果为true,则返回登录计数,或者返回文本 。
The method creates a singleton instance of the class and then calls the method to register the various action and filter hooks.
方法创建该类的单例实例,然后调用方法来注册各种操作和过滤器挂钩。
Finally, we’ll make a call to the method to put the PHP class to work.
最后,我们将调用方法以使PHP类正常工作。
Voila! We are done coding our login counter plugin.
瞧! 我们已经完成了登录计数器插件的编码。
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/