WordPress插件制作教程(一): 如何创建一个插件

上一篇还是按照之前的教程流程,写了一篇WordPress插件制作教程概述,从这一篇开始就为大家具体讲解WordPress插件制作的内容。这一篇主要说一下插件的创建方法。

相信大家都知道插件的安装文件在什么地方吧,没错就在WP-Content->plugins里面,我们所安装的插件都存放在了这个文件夹里面。当我们刚开始搭建好WordPress网站的时候,里面会默认提供两个插件,一个是Akismet(过滤垃圾评论插件)和一个hello插件(显示歌词的插件)。我们可以打开hello.php这个文件,这个插件相当于我们制作插件的入口,通过查看里面的内容,就可以知道创建一个插件的方法。如下:

<?php
/**
 * @package Hello_Dolly
 * @version 1.6
 */
/*
Plugin Name: Hello Dolly
Plugin URI: http://wordpress.org/plugins/hello-dolly/
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
Author: Matt Mullenweg
Version: 1.6
Author URI: http://ma.tt/
*/

// 上面分别是 插件的名称,插件URL地址,插件描述,插件作者,插件版本,作者地址. 这些内容使用 '/*  */' 注释符号括住

这里要注意的是你创建的插件名称和插件文件夹名称必须是唯一的,独一无二的,这样避免与其他插件发送冲突。可以去Google或者百度先验证一下这个名字到底是不是独一无二的。还有就是你的取的插件名字得让别人明白你的插件是干什么的,文件夹名称不能使用中文名称,下面就简单的说一下流程。

首先你需要考虑所制作插件的复杂度,如果很简单可以直接创建一个文件,如果涉及的文件较多,需要创建一个文件夹。不管哪种需要名称的唯一性,比如创建一个插件文件夹名为my_plugin,然后在文件中创建下面的信息。

/**
 * @package Hello_Dolly
 * @version 1.6
 */
/*
Plugin Name: My Plugin
Plugin URI: http://www.myplugin.com
Description: 我制作的第一个WP插件
Author: myname
Version: 1.0
Author URI: http://www.cnblogs.com/fxmbz
*/

标准的插件信息至少要有插件名称,这样WordPress才能识别你的插件。其他信息将显示在控制面板插件管理页面中。 标准插件信息对各行顺序没有要求。 创建好之后你的后台便会出现你刚刚创建的插件。这样你的插件就算创建成功了,还有一般在插件信息的下面可以添加版权信息。

/*
  Copyright 年份  作者名  (email : 你的邮箱)

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

如果大家想把插件提交到WordPress官网(插件提交与推广参考:https://codex.wordpress.org/Plugin_Submission_and_Promotion),或者给用户有一个很好的说明。可以添加一个名称为 Readme.txt 的文件。里面可以介绍插件的功能、安装方法、使用说明、适用的WordPress版本、插件更新信息等。

插件的创建还是比较容易的。大家可以根据自己的习惯和需求添加一些其它内容,比如可以写一个html页面专门来介绍你的插件。还有就是在开始制作插件之前多多研究下已有插件的写法,每个插件的制作方法千变万化,如果有不错的方法要及时做好总结。这样我们在开发的过程中可以少走很多弯路。

 

本章总结:

1. WordPress插件,文件放置的目录:wordpress/wp-content/plugins/myplugin/myplugin.php

2. WordPress插件,的声明范本

3. 实现简单的插件功能(在wp后台头部输出自定义字符串)

/**
 * @package My Plugin
 * @version 1.6
 */
/*
Plugin Name: My Plugin
Plugin URI: http://www.cnblogs.com/fxmbz/p/4059678.html
Description: 我制作的第一个WP插件,这个插件就是在后台页面的头部显示一段文字
Author: zhangxl
Version: 1.0
Author URI: http://www.cnblogs.com/fxmbz
*/

/*
  Copyright 年份  作者名  (email : 你的邮箱)

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

// 在wp后台头部输出自定义的字符串
add_action('admin_head', 'my_first_plugin');
function my_first_plugin() {
    echo '<h1>我制作的第一个WP插件</h1>';
}

 

0 0 投票数
文章评分
订阅评论
提醒
guest

0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x