开发 WordPress 插件 教程 – 插件是如何工作的?


WORDPRESS 博客强大的地方 就在于可以用插件 扩展功能. WORDPRESS有一个专门管理插件的页面 可以很清楚很简单的对每个插件进行 升级, 配置, 开启和关闭等操作.

插件目录

所有插件都存于/wp-content/plugins 目录下. 每个插件都有自己的目录. 目录下必须要有同名的 PHP 文件 作为插件的入口. 比如你创建了文件夹 sample 在里面就必须有 sample.php 作为插件的入口.

插件的描述信息

插件的描述信息需要在 PHP 文件的最顶段 以注释的方式指定, 比如:

1
2
3
4
5
6
7
8
9
10
<php
/*
Plugin Name: 插件名称
Description: 插件描述
Version: 0.1
Author: justyy
Author URI: https://justyy.com
License: Free
Text Domain: sample-plugin
*/
<php
/*
Plugin Name: 插件名称
Description: 插件描述
Version: 0.1
Author: justyy
Author URI: https://justyy.com
License: Free
Text Domain: sample-plugin
*/

例子 – 第一个插件

之前在 这里 提到用 HTTP_REFERER 来简单有效的过滤垃圾评论的方法. 我们可以简单的把它弄成一个插件 而不是直接修改 functions.php 模板文件.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< ?php
/*
Plugin Name: Check Comment Flood
Description: This Simple Plugin prevents most of the spam comments by checking the HTTP_REFERER variable. No complex configurations just simply activating the plugin will do the job. 
Version: 0.1
Author: justyy
Author URI: https://justyy.com
Plugin URI: 
License: Free
Text Domain: check-comment-flood 
*/
 
function check_referrer() {
    if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == '') {
        wp_die(__('Please do not access this file directly.'));
    }
}
add_action('check_comment_flood', 'check_referrer');
< ?php
/*
Plugin Name: Check Comment Flood
Description: This Simple Plugin prevents most of the spam comments by checking the HTTP_REFERER variable. No complex configurations just simply activating the plugin will do the job. 
Version: 0.1
Author: justyy
Author URI: https://justyy.com
Plugin URI: 
License: Free
Text Domain: check-comment-flood 
*/

function check_referrer() {
	if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == '') {
		wp_die(__('Please do not access this file directly.'));
	}
}
add_action('check_comment_flood', 'check_referrer');

保存插件文件 然后在WORDPRESS里就能看到了.

first-wordpress-plugin 开发 Wordpress 插件 教程 - 插件是如何工作的? PHP是最好的语言 wordpress 小技巧 程序设计 编程 网站信息与统计

first-wordpress-plugin

这种方式有啥好处?

可以随时对插件 开启或关闭. 把插件功能提取出来可以方便日后对其维护(升级). 升级主题文件或者WORDPRESS版本不会影响插件的功能. 另一个好处就是 如果直接修改 functions.php 改坏了 整个WORDPRESS就不能运行了. 但是如果改坏了 插件, WORDPRESS会把插件禁用了 不会影响到其它的功能的使用, 是个比较安全的做法.

github: https://github.com/DoctorLai/check-comment-flood

英文: WordPress Plugin Tutorial – How to Write First Plugin?

Wordpress博客技术文章

GD Star Rating
loading...
本文一共 376 个汉字, 你数一下对不对.
开发 WordPress 插件 教程 – 插件是如何工作的?. (AMP 移动加速版本)
上一篇: 推荐英国 Holland & Barrett 减肥茶 - Bootea
下一篇: 在WORDPRESS管理员界面上面添加菜单选项

扫描二维码,分享本文到微信朋友圈
f2bd772fcccdfe481d1480b7e1291425 开发 Wordpress 插件 教程 - 插件是如何工作的? PHP是最好的语言 wordpress 小技巧 程序设计 编程 网站信息与统计

评论