這一篇主要說一下插件的創(chuàng)建方法。
相信大家都知道插件的安裝文件在什么地方吧,沒錯(cuò)就在WP-Content->plugins里面,我們所安裝的插件都存放在了這個(gè)文件夾里面。當(dāng)我們剛開始搭建好WordPress網(wǎng)站的時(shí)候,里面會(huì)默認(rèn)提供兩個(gè)插件,一個(gè)是Akismet(過濾垃圾評(píng)論插件)和一個(gè)hello插件(顯示歌詞的插件)。我們可以打開hello.php這個(gè)文件,這個(gè)插件相當(dāng)于我們制作插件的入口,通過查看里面的內(nèi)容,就可以知道創(chuàng)建一個(gè)插件的方法。如下:
<?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 Hello, Dolly in the upper right of your admin screen on every page. Author: Matt Mullenweg Version: 1.6 Author URI: http://ma.tt/ */ // 上面分別是 插件的名稱,插件URL地址,插件描述,插件作者,插件版本,作者地址. 這些內(nèi)容使用 '/* */' 注釋符號(hào)括住
這里要注意的是你創(chuàng)建的插件名稱和插件文件夾名稱必須是唯一的,獨(dú)一無二的,這樣避免與其他插件發(fā)送沖突。可以去Google或者百度先驗(yàn)證一下這個(gè)名字到底是不是獨(dú)一無二的。還有就是你的取的插件名字得讓別人明白你的插件是干什么的,文件夾名稱不能使用中文名稱,下面就簡單的說一下流程。
首先你需要考慮所制作插件的復(fù)雜度,如果很簡單可以直接創(chuàng)建一個(gè)文件,如果涉及的文件較多,需要?jiǎng)?chuàng)建一個(gè)文件夾。不管哪種需要名稱的唯一性,比如創(chuàng)建一個(gè)插件文件夾名為my_plugin,然后在文件中創(chuàng)建下面的信息。
/** * @package Hello_Dolly * @version 1.6 */ /* Plugin Name: My Plugin Plugin URI: http://www.myplugin.com Description: 我制作的第一個(gè)WP插件 Author: myname Version: 1.0 Author URI: http://www.cnblogs.com/fxmbz */
標(biāo)準(zhǔn)的插件信息至少要有插件名稱,這樣WordPress才能識(shí)別你的插件。其他信息將顯示在控制面板插件管理頁面中。 標(biāo)準(zhǔn)插件信息對(duì)各行順序沒有要求。 創(chuàng)建好之后你的后臺(tái)便會(huì)出現(xiàn)你剛剛創(chuàng)建的插件。這樣你的插件就算創(chuàng)建成功了,還有一般在插件信息的下面可以添加版權(quán)信息。
/* 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官網(wǎng)(插件提交與推廣參考:https://codex.wordpress.org/Plugin_Submission_and_Promotion),或者給用戶有一個(gè)很好的說明。可以添加一個(gè)名稱為 Readme.txt 的文件。里面可以介紹插件的功能、安裝方法、使用說明、適用的WordPress版本、插件更新信息等。
插件的創(chuàng)建還是比較容易的。大家可以根據(jù)自己的習(xí)慣和需求添加一些其它內(nèi)容,比如可以寫一個(gè)html頁面專門來介紹你的插件。還有就是在開始制作插件之前多多研究下已有插件的寫法,每個(gè)插件的制作方法千變?nèi)f化,如果有不錯(cuò)的方法要及時(shí)做好總結(jié)。這樣我們?cè)陂_發(fā)的過程中可以少走很多彎路。
本章總結(jié):
1. WordPress插件,文件放置的目錄:wordpress/wp-content/plugins/myplugin/myplugin.php
2. WordPress插件,的聲明范本
3. 實(shí)現(xiàn)簡單的插件功能(在wp后臺(tái)頭部輸出自定義字符串)
/** * @package My Plugin * @version 1.6 */ /* Plugin Name: My Plugin Plugin URI: http://www.cnblogs.com/fxmbz/p/4059678.html Description: 我制作的第一個(gè)WP插件,這個(gè)插件就是在后臺(tái)頁面的頭部顯示一段文字 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后臺(tái)頭部輸出自定義的字符串 function my_first_plugin() { echo '我制作的第一個(gè)WP插件'; } add_action('admin_head', 'my_first_plugin');
課后作業(yè)
請(qǐng)將下文給出的代碼制作成插件并成功運(yùn)行。