15 個設定 WordPress 網站的程式碼整理,加入 wp-config.php 就能使用



15 個設定 WordPress 網站的程式碼整理,加入 wp-config.php 就能使用15 個設定 WordPress 網站的程式碼整理,加入 wp-config.php 就能使用

本文參考資料為 15 wp-config Snippets to Configure Your WordPress Site, 原作者 Preethi Ranjit。

現在遇到想架站的朋友,我都二話不說直接推薦使用 WordPress,大家都知道 WordPress 分成兩種:WordPress.com 和 WordPress.org(或被稱為 Self-hosted),前者如同 Blogger 是一個免費網誌平台,後者則是開放原始碼程式,必須搭配網域名稱和虛擬主機才能夠把網站架設起來。每次搬家轉換都會有所損耗,無論是時間、金錢或好不容易累積起來的流量及搜尋引擎最佳化成績,還是那句老話「長痛不如短痛」,最好能一開始就把方向設定清楚。

之前陸續寫過一些 WordPress 相關的書籍,趁機打個廣告,如果你想學習 WordPress 自架站的話,可參考我翻譯的《WordPress 無敵架站手冊》,有非常完整全面且循序漸進的章節規劃,對於探索 WordPress 來說是個相當適合入門的書籍。

我也利用空檔寫了一些可讓 WordPress 網站變得更快、更安全的教學主題,包括:

  • 殺手級 WordPress 體檢項目:101 個建立新網站的超簡易捷徑!
  • 11 個強化 WordPress 網站安全的 .Htaccess 設定技巧
  • 8 個實用的 WordPress 程式碼(Code Snippets)
  • 安裝 WordPress 後你應該做的 25 件事

WordPress 最為人津津樂道的是安裝過程非常簡單!如果夠熟悉的話甚至可以在幾分鐘時間快速部屬一個網站,即使如此,WordPress 仍具有相當高的彈性,可透過程式碼來調整網站各項設定。

有些設定參數不一定每個人都知道,本文要介紹的 15 個程式碼都是使用於 WordPress 設定檔(wp-config.php)中,不過有些跟之前我寫過的教學重複,但沒關係,若是需要就直接把它複製到自己的設定檔吧!以下我會就這十五個項目做更詳細的說明。

本地化及編輯你的 wp-config

當你下載 WordPress 後,wp-config.php 檔案並不會在安裝資料夾。但你可以找到一個名為 wp-config-sample.php 的範例檔,你必須將它複製、重新命名為 wp-config.php,然後加入基本的資料庫連線資訊(資料庫名稱、使用者名稱、密碼、網址及安全金鑰)。

如果你的主機商提供 Softaculous 或類似的自動化安裝工具,那麼安裝過程會自動處理這些必要步驟,因此你在以 FTP 連線至你的伺服器時將會看到 wp-config.php 和 wp-config-sample.php 同時存在於根目錄。

15 個設定 WordPress 網站的程式碼15 個設定 WordPress 網站的程式碼

請注意,設定順序很重要,因此請不要重新排列。如要編輯 wp-config 請務必使用程式碼編輯器(例如:Sublime Text、Atom、Notepad++、Visual Studio Code 或 TextMate),使用 Word 及其他類似編輯器(例如:Microsoft Office、Google Docs、LibreOffice)將會破壞你的檔案,千萬別用它們來編輯程式碼。

保存在 wp-config 的設定值會覆蓋資料庫設定值,以防止兩種設定類型同時存在。

這些程式碼要放在那裡?

在這篇文章中,你會找到 15 個可放在 wp-config.php 設定檔裡的程式碼。

大多數情況下這些設定選項並不存在於 wp-config 中。如果你想使用它們,必須將它複製並新增到 <?php 標籤及代碼註解之後,或是 MySQL 設定值之前

1. 開啟 WP 除錯工具(Debugger)

你可以在 wp-config 設定檔內開啟或關閉 WordPress 除錯工具。下面要介紹的第一個程式碼預設情況下已經存在於 wp-config.php(在資料庫設定底下),但預設值為 false。如果要將除錯工具開啟,將設定值設定為 true

第二段程式碼是開啟前端除錯工具,允許你針對 CSS 和 JavaScript 程式進行除錯。只在開發中的網站下使用除錯工具。

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 開啟 PHP 除錯工具
define( 'WP_DEBUG', true );
# 開啟 CSS 和 JavaScript 除錯工具
define( 'SCRIPT_DEBUG', true );

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

2. 更改資料表前綴

預設情況下 WordPress 使用 wp_ 作為資料表前綴。如果你想要提高安全性,可以選擇一個更複雜的資料表前綴。

此選項預設也存在於 wp-config 設定檔中,你只要將 $table_prefix 變更成其他值就能獲得更好的安全性。

不過這只有在一個全新安裝開發網站才可變更,在運作中的網站更改這個參數會有風險。

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 為資料庫建立更安全的資料表前綴
# 只能使用數字、英文字母和底線
$table_prefix = 'a81kJt_';

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

3. 更改 WordPress 網址

你可以在 WordPress 控制台的「設定 -> 一般」裡設定 WordPress 和網站網址。此外,你也可以在 wp-config 設定檔裡設定這些網址。

在 wp-config.php 裡定義 WP_SITEURL 和 WP_HOME 有兩個原因:

  1. 如果因為某些原因無法存取控制台,這個方法有可能解決問題。
  2. 減少網站載入時呼叫資料庫的次數(因為 wp-config 會覆蓋資料庫相同選項)。

WP_SITEURL 指定使用者連結到你網站的網址,WP_HOME 則是 WordPress 安裝目錄。如果你將 WordPress 安裝於根目錄(這是預設選項),那麼這兩個設定值會相同。

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 指定網站網址
define('WP_SITEURL', 'http://www.yourwebsite.com');
# 指定 WordPress 網址(安裝目錄)
define('WP_HOME', 'http://www.yourwebsite.com/wordpress');

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

4. 在一段時間後清理回收桶

你可以讓 WordPress 在一段時間後自動清理你的回收桶。這個選項最小值為 0,在這情況下會停用垃圾桶功能。

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 在七天後清空回收桶
define( 'EMPTY_TRASH_DAYS', 7 );

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

5. 啟用 WordPress 快取

你可以啟用 WordPress 內建快取功能,只要加入以下程式碼。大多數快取外掛,例如 W3 Total Cache 和 WP Super Cache 都會自動將這段程式碼加入 wp-config 設定。

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 啟用 WP 快取
define( 'WP_CACHE', true );

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

6. 啟用 WordPress 多網站(Multisite)

在你的 wp-config 設定檔加入 WP_ALLOW_MULTISITE 可啟用 WordPress 多網站功能,讓你建立一個網誌網路

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 啟用 WordPress 多網站
define( 'WP_ALLOW_MULTISITE', true );

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

7. 重新導向不存在的子網域和子目錄

有時候訪客會在網址列輸入一個不存在的子網域名稱或子目錄。你可以將這些訪客重新導向至正確的網域名稱,例如加入 NOBLOGREDIRECT 參數來重新導向到網站首頁。

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 重新導向不存在的子網域和子目錄
define( 'NOBLOGREDIRECT', 'http://www.yourwebsite.com' );

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

8. 管理文章版本

WordPress 有內建的版本控制系統,意味著它可以儲存你建立的所有文章版本。一篇頻繁更新的文章可能會有 25-30 個文章版本,一段時間後這會佔用大量的資料庫空間。

使用 WP_POST_REVISIONS 參數可以設定文章版本的最大數量,或將此功能完全停用。

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 完全停用文章版本功能
define( 'WP_POST_REVISIONS', false );
# 只允許最多五個文章版本
define( 'WP_POST_REVISIONS', 5 );

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

9. 啟用內建資料庫最佳化工具

WordPress 有內建的資料庫最佳化功能,你可以將以下程式碼加入 wp-config 來啟用它。

如果想知道這個工具的詳細運作原理,可參考國外使用者的文章介紹(英文)。不過更重要的是資料庫最佳化畫面會被所有人看見(即使是沒有登入的訪客)。因此建議只在需要時啟用,而且使用後別忘記將它關閉。

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 啟用資料庫最佳化功能
define( 'WP_ALLOW_REPAIR', true );

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

10. 停用自動更新

WordPress 預設情況下會執行自動背景更新,主要針對重要安全性更新翻譯內容

你可以透過 AUTOMATIC_UPDATER_DISABLED(所有更新)和 WP_AUTO_UPDATE_CORE(核心更新)將這項功能開啟或停用。

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 停用所有自動更新
define( 'AUTOMATIC_UPDATER_DISABLED', true );
# 停用所有核心更新
define( 'WP_AUTO_UPDATE_CORE', false );
# 啟用所有核心更新,包括跨版本更新及主要更新
define( 'WP_AUTO_UPDATE_CORE', true );
# 啟用核心更新,但只針對安全性更新(預設)
define( 'WP_AUTO_UPDATE_CORE', 'minor' );

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

11. 增加 PHP 記憶體限制

有時候你希望可以增加主機商對你網站的 PHP 記憶體限制,特別是出現可怕的「Allowed memory size of xxxxxx bytes exhausted」訊息。對網站的記憶體限制可以使用 WP_MEMORY_LIMIT 參數調整,WP_MAX_MEMORY_LIMIT 則針對控制台。

請注意,有些主機商不允許手動增加記憶體限制,遇到這種情形時請聯繫他們尋求協助。

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 為網站設定記憶體限制
define( 'WP_MEMORY_LIMIT', '96M' );
# 為控制台設定記憶體限制
define( 'WP_MAX_MEMORY_LIMIT', '128M' );

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

12. 強制 SSL 模式登入

如果想增強網站安全性,你可以強制每次都使用 SSL 進行登入FORCE_SSL_ADMIN 參數可以針對登入頁面控制台開啟完整的 SSL 加密支援。

請注意,WordPress 4.0 後已經棄用 FORCE_SSL_LOGIN,現在必須使用 FORCE_SSL_ADMIN 。

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 強制 SSL 模式登入
define( 'FORCE_SSL_ADMIN', true );

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

13. 停用外掛和佈景主題編輯/更新

管理員可以在 WordPress 控制台編輯外掛和佈景主題檔案。如果你使用 DISALLOW_FILE_EDIT 來停用外掛和佈景主題編輯器,有助於讓你的網站更安全。所以,若你的網站遭到入侵,駭客將沒有權限存取你的外掛和佈景主題檔案。

你也可以使用 DISALLOW_FILE_MODS 來停用外掛和佈景主題更新功能。這麼一來管理員就無法在控制台更新外掛和佈景主題。

DISALLOW_FILE_MODS 也會停用外掛及佈景主題編輯器,所以如果你啟用這項設定,就不用再額外加入 DISALLOW_FILE_EDIT

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 停用外掛和佈景主題編輯器
define( 'DISALLOW_FILE_EDIT', true );
# 停用外掛和佈景主題編輯器,加上外掛和佈景主題更新
define( 'DISALLOW_FILE_MODS', true );

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

14. 刪除圖片編輯

每當你編輯圖片時,WordPress 會將它儲存成不同解析度大小的圖片。但是,如果你不想使用早期的圖片集,可以將 IMAGE_EDIT_OVERWRITE 設定為 true 來移除他們。

如你所見,當你編輯圖片時,較早的圖片將會被新圖片覆蓋,只有最新的圖片會保存於 wp-content 資料夾。

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 清理圖片編輯
define( 'IMAGE_EDIT_OVERWRITE', true );

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

15. 停用未篩選的 HTML

這裡我要稍微解釋一下,什麼是「Unfiltered HTML」呢?以中文來說就是未被篩選的 HTML,為了有更好的安全性,WordPress 不允許等級較低的使用者帳戶(訂閱者、寫手或作者)發佈未篩選的 HTML 語法(這些語法在寫入編輯器、儲存後會自動消失),然而編審和系統管理員是允許使用這些標籤的。

如果你想將安全性提高,禁止更高等級的使用者使用未篩選的 HTML 語法,可以將以下程式碼加入到 wp-config 設定檔。

.gist table { margin-bottom: 0; }


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

# 禁止編審和系統管理員使用未篩選的 HTML
define( 'DISALLOW_UNFILTERED_HTML', true );

view raw

gistfile1.php

hosted with ❤ by GitHub

 

Ten articles before and after

LINE 網頁版又回來了!打開瀏覽器就能跟好友聊天

開啟 LINE 鎖定模式,離開時自動上鎖對話視窗(Windows、Mac)

關閉 Google Chrome、Firefox 通知提示,避免被網站要求權限打擾

殺手級 WordPress 體檢項目:101 個網站必須遵照的原則!

Web Launch Checklist 網站上線前你應該完成的工作檢查清單

在網站搬家前,你應該先註冊一個自己的網址!四個推薦的網域名稱註冊商

Facebook 假帳號橫行!如何避免被陌生人亂加好友干擾?

男人別再亂加好友推薦的臉書正妹,其實很多都是「殭屍帳號」…

設定讓 Google Chrome 可直接顯示 SSL 憑證資訊教學

Flume for Mac 最好用的 Instagram 電腦版免費下載!支援上傳相片及私人訊息等完整功能