Zum Inhalt

WordPress 建立支援 function.php 的自定義插件

WordPress 建立支援 Function.php 的自定義插件

為自己的網站增加特定功能的代碼只能寫進當前主題的 function.php,但麻煩就是換了主題,就要記得將代碼從新寫進新主題的 function.php。要測試代碼也沒有像插件管理來的方便,可以直接啟動與停用。google了一下大多建議要修改 Function.php 的話,使用自定義插件會比較方便,而且建立自定義插件也很簡單。以 hello dolly 這個插件為範例看了一下代碼,建立簡單的插件果然很傻瓜模式。

建立自定義插件

創見一個 名稱自己取.php 的文件,這邊範例 My Plugin.php,然後將以下的代碼複製進去存檔

<?php
/**
* @package My Plugin
* @version 1.0
*/
/*
Plugin Name: My Plugin
Description: This is just a plugin for special functions, which will used for my website 
Author: nonameslife
Version: 1.0
*/

/* Start adding Functions Below this Line 在以下空白處加入代碼 */


/* Stop Adding Functions Below this Line*/

這邊沒用 ?> 結尾,因為純 PHP 代碼文件可以省略,例如 hello dolly.php 也是這樣。紅字部份描述會顯示插件管理介面上。

然後將 My Plugin.php 放到 wp-content\plugins 的資料夾裡面就好了。

編輯 My Plugin.php

登入 WordPress,在插件選項裡選擇插件編輯器,選取自定義插件後就能進行編輯。在插件選單裡可以啟動與停用

參考資料:

wordpress codex

How To Safely Add PHP Code To WordPress

Best methods to Insert Custom PHP code in WordPress?

Schlagwörter:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.