Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
如何創建一個簡單的 WordPress 外掛,外掛將在每篇文章的底部添加一個自定義訊息。這個教學適合初學者或任何對學習 WordPress 外掛開發感興趣的人。
首先,確保你已經擁有一個運行中的 WordPress 網站。如果你還沒有,可以參考 WordPress 官方網站 上的指南來設置一個。
wp-content/plugins/
目錄。simple-message-plugin
。simple-message-plugin
資料夾中,創建一個名為 simple-message-plugin.php
的檔案。simple-message-plugin.php
文件,並插入以下程式碼:<?php
/**
* Plugin Name: 簡單訊息外掛
* Description: 簡單的外掛功能測試,在每偏文章加入文字
* Version: 1.0
* Author: 外掛作者名稱
*/
// 這個功能將會加上自訂的訊息
function add_custom_message($content) {
$custom_message = "<div style='color: blue; border-top: 1px solid #000; padding-top: 10px;'>
<p>這個訊息是簡單訊息外掛的測試文字。</p>
</div>";
if(is_single()) { // 確認是否是單篇文章格式
$content .= $custom_message; // 加上自定義的內容
}
return $content; // 回傳內容
}
// 將功能掛載在內容區塊
add_filter('the_content', 'add_custom_message');
?>
現在,當你瀏覽任何單篇文章時,你應該在文章底部看到一個自定義的訊息。這個簡單的外掛教你如何創建基本的 WordPress 外掛並了解如何使用掛鉤(hooks)來修改 WordPress 的功能。