WordPress外掛開發基礎
外掛主要的目的有幾個:
- 提供功能函式,供其他的外掛使用
- 在後台儀表板寫管理功能(增加管理功能菜單,例如訂單管理、權限管理…)
- 提供短碼,供頁面嵌入需要的shortcode,像ultimate shortcode這個外掛。
短碼外掛過程:
- 在plugins資料夾中建立一個外掛目錄(例: my-plugin)
- 在目錄下建立一個與新建立的外掛目錄一樣的主檔(例:my-plugin.php)
- 主檔my-plugin.php中加上外掛定義與說明註解
- 建立shortcode函式(例:myShortcode)
- 在頁面設計中嵌入需要shortcode,例:[myShortcode]…
建立外掛的基礎認知:
- 外掛的命名:WordPress外掛為數眾多,當撰寫的外掛跟其他的外掛名稱衝突,就會影響到這些同名外掛函式,撰寫外掛最重要的一件事就是為外掛取一個獨一無二的名稱,一般的作法就是用自己的名字做為命名的前綴(前置, prefix),而你的名字有沒有跟別的外掛作者同名…,可以試著用Google搜尋是否有與自己同名,對使用中文的我們,可以用自己姓名的羅馬拼音,應該可以有極大機率避免同名。
- 建立一個外掛所需檔案,外掛是建立在wp-contentsplugins目錄下,一個外掛需要建立一個單獨的目錄來放置外掛所有的檔案。
- 在主要的PHP檔案中加入檔頭資訊
/* Plugin Name: 外掛名稱 Plugin URI: 外掛的首頁,通常是用來介紹外掛、文件、販售訊息 Description: 簡短的外掛描述 Version: 版本資訊 Author: 外掛作者名稱 Author URI: 外掛作者的網頁 License: GPL2 etc 版權宣告 License URI: 外掛的授權資訊網頁連結 */在上面的檔頭之後,我們需要加上版權宣告,如果我們使用的是GPL2或GPL2相容的版權:
/* Copyright 年 外掛作者姓名 (email : 你的email地址) (外掛名字) 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 any later version. (外掛名字) 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 (外掛名字). If not, see (http://link to your plugin license 你的外掛授權連結). */
WP Renym外掛實作細節
本文參考自: 外掛的命名,首先我們需要搜尋WordPress的外掛庫,並且搜尋Google是否我們的外掛名字有跟其他名字衝突,WP Renym這個名字是OK的,作者原先取名WP Rename,但是發現已經有相同的名字了。 接著,使用編輯器新增一個檔案,輸入:/* Plugin Name: WP Renym Plugin URI: http://link to your plugin homepage Description: This plugin replaces words with your own choice of words. Version: 1.0 Author: Freddy Muriuki Author URI: http://link to your website License: GPL2 etc License URI: https://link to your plugin license Copyright YEAR PLUGIN_AUTHOR_NAME (email : your email address) (Plugin Name) 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 any later version. (Plugin Name) 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 (Plugin Name). If not, see (http://link to your plugin license). */將這個檔案儲存為wp-renym.php,並且檔案的資料夾為 wp-renym。
加入functions
function renym_wordpress_typo_fix( $text ) { return str_replace( 'wordpress', 'WordPress', $text ); } add_filter( 'the_content', 'renym_wordpress_typo_fix' );renym_wordpress_typo_fix 是獨一無二的函式名悄,當增加functions時,永遠不要以wp_起頭, 這樣的話,可以防止之後那些使用wp_前置詞的函式衝突到。 上面這個函式使用 $text 做為參數,函式回傳將$text中的 ‘wordpress’字串置換為’WordPress’。 接著,我們加了一個filter ( add_filter ) 到我們的外掛,這樣的動作是使用我們的過濾器將文章的內容 ( the_content ) 進行處理,也就是將文章裏的 ‘wordpress’字串置換為’WordPress’。 To replace more than one word (perhaps you would like to edit multiple words across your blog or use the plugin as a simple profanity filter), replace the above code with the following code: 為了置換文章內容中更多的字詞,我們可將上面的程式碼置換為:
function renym_content_replace( $content ) { $search = array( 'wordpress', 'goat', 'Easter', '70', 'sensational' ); $replace = array( 'WordPress', 'coffee', 'Easter holidays', 'seventy', 'extraordinary' ); return str_replace( $search, $replace, $content ); } add_filter( 'the_content', 'renym_content_replace' );上面的程式碼,使用陣列的方式定義要被置換的字串陣列$search與將置換的字串陣列$replace。 在所有的函式將上獨一無二的前置詞renym,可以避免函式名稱衝突(與其他函式同名) 。 上面的程式完成了文章內容的置換過濾器,接著我們在所有的文章尾巴加上”感謝您閱讀此教學…”, 這個函式我們可以放在另一個主要的外掛檔案renym_content_replace .php 中:
function renym_content_footer_note( $content ) { $content .= '<footer class="renym-content-footer">感謝您閱讀此教學, 或許下次有機會能讓您請我喝一杯咖啡!更多的WordPress教學文章請訪問我們的<a href="http://wpexplorer.com/blog" title="WPExplorer Blog">Blog</a></footer>'; return $content; } add_filter( 'the_content', 'renym_content_footer_note' );renym_content_footer_note函式把$content這個參數加上HTML元素後回傳,加上的的HTML是一個footer標籤,標籤內有一個renym-content-footer類別,我們可以在CSS中進行該類別的格式設置。 程式後面加入一個過濾器 ( add_filter ) 告知我們的函式操作所選取的文字,這個文字也就是文章裏的內容the_content。
Compress Your Folder/壓縮資料夾
這個時間點,最終的 wp-renym.php 檔案應該看起來如下:<?php /* Plugin Name: WP Renym Plugin URI: http://link to your plugin homepage Description: This plugin replaces words with your own choice of words. Version: 1.0 Author: Freddy Muriuki Author URI: http://link to your website License: GPL2 etc License URI: https://link to your plugin license Copyright YEAR PLUGIN_AUTHOR_NAME (email : your email address) (Plugin Name) 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 any later version. (Plugin Name) 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 (Plugin Name). If not, see (http://link to your plugin license). */ /*Use this function to replace a single word*/ function renym_wordpress_typo_fix( $text ) { return str_replace( 'wordpress', 'WordPress', $text ); } add_filter( 'the_content', 'renym_wordpress_typo_fix' ); /*Or use this function to replace multiple words or phrases at once*/ function renym_content_replace( $content ) { $search = array( 'wordpress', 'goat', 'Easter', '70', 'sensational' ); $replace = array( 'WordPress', 'coffee', 'Easter holidays', 'seventy', 'extraordinary' ); return str_replace( $search, $replace, $content ); } add_filter( 'the_content', 'renym_content_replace' ); /*Use this function to add a note at the end of your content*/ function renym_content_footer_note( $content ) { $content .= '<footer class="renym-content-footer">Thank you for reading this tutorial. Maybe next time I will let you buy me a coffee! For more WordPress tutorials visit our <a href="http://wpexplorer.com/blog" title="WPExplorer Blog">Blog</a></footer>'; return $content; } add_filter( 'the_content', 'renym_content_footer_note' ); ?>這時候確認所有的變更都已儲存,此時壓縮資料夾,名稱為wp-renym.zip,注意,檔案必須是ZIP,否則這個外掛無法安裝。
Use Your Plugin
到後台管理界面->外掛->安裝外掛,上傳我們的外掛壓縮檔,並且啟用這個外掛,此時你應該可以看到一下的畫面參考文獻:
- WordPress Plugin Development from Scratch
- The Right Way to Customize a WordPress Plugin
- Getting Started with WordPress Plugin Development
- Writing a Simple WordPress Plugin, Beginner Tutorial
- Writing a Plugin – WordPress Codex
- Plugins – WordPress Codex
- Plugin Development – WPMU
- Getting Started With WordPress Plugin Development
- How To Write A Simple Redirect Plugin
- INTRO TO WORDPRESS PLUGIN DEVELOPMENT
- Including JavaScript in Plugins or Themes, the Right Way
- WordPress Plugin Development Resources and Tutorials
- WordPress Plugin Tutorial: How to Create a WordPress Plugin
- WordPress Plugin Development Resources, Tutorials and Guides
Shortcode
外掛透過自定義的”Shortcode”將輸出顯示到靜態頁面或文章 (編輯時,加入自定義shortcode)- Create a simple WordPress Plugin with Shortcode
- The Beginner’s Guide to Writing Your Own Custom Shortcode
- The Ultimate Guide to WordPress Shortcodes (With Examples to Create Your Own)
- The Complete Guide to Creating Custom Shortcodes in WordPress
- The Shortcode API