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>';
}

 

下一章我們繼續講解:編寫一個簡單的插件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM