網頁

2015年6月28日 星期日

無法重新定義pluggable function: wp_notify_postauthor()

最近接了個維護案,做了更新後,重新啟動plugin,卻出現底下的訊息

Fatal error: Cannot redeclare wp_notify_postauthor() (previously declared in /ae/wp-includes/pluggable.php:1003) in /ae/wp-content/plugins/ae-menu-pages/ae-menu-pages.php on line 120

本以為是更新後造成問題,但查半天發現主要是重新定義wp_notify_postauthor造成的,這個function是個pluggable function,讓開發者可以在plugin中重新定義的,看半天並不覺得有任何問題,直到查到這篇How to Override a Pluggable Function

原文解說如下:
the reason it needs to be wrapped in function_exists is because within the context of a plugin activation, the function does already exist. This is due to the way WordPress activates plugins in a sandbox, which is necessary to enable it to recover from fatal errors in the activated plugin. Technically, once the plugin is activated the function_exists check would not be necessary, but you'd never be able to activate that plugin via conventional means without it. –  Milo Sep 24 '12 at 15:57

試了後確定是這個問題,總之,不管是否這個function是在wordpress載入預設的同名function前就宣告了,就是一定要加入function_exists這個判斷如下:
if( !function_exists() ):
    function wp_notify_postauthor() {
        //執行內容
    }
endif;


參考網址:
How to Override a Pluggable Function
Customizing the new user email with a pluggable function

沒有留言:

張貼留言