2011-04-11 33 views
0

我在寫一個使用wp_mail函數的插件。不過,我想更改From:地址。 WP提供了一些過濾器 - wp_mail_from_namewp_mail_from - 但我不知道如何從一個類中調用它們。從一個類中的apply_filters(WordPress)

如果我把它們放在一個函數之外,會有一個解析錯誤(意外的T_STRING,期待T_FUNCTION)。

如果我把它們的功能沒有內似乎發生

class myPlugin {  
    public function setFromname($fromname) { 
     apply_filters('wp_mail_from_name', $fromname); 
     $this->fromname = $fromname; 
    } 
    public function setFromemail($fromemail) { 
     apply_filters('wp_mail_from', $fromemail); 
     $this->fromemail = $fromemail; 
    } 
} 

怎麼可能給一個類中影響這些過濾器?

回答

2

在WordPress過濾器必須有一個回調,他們不能使用一個變量。

class myPlugin { 
    public function myPlugin { 
     add_filter('wp_mail_from_name', array($this, 'filter_mail_from_name')); 
     add_filter('wp_mail_from', array($this, 'filter_mail_from')); 
    } 

    function filter_mail_from_name($from_name) { 
     // the $from_name comes from WordPress, this is the default $from_name 
     // you must modify the $from_name from within this function before returning it 
     return $from_name; 
    } 

    function filter_mail_from($from_email) { 
     // the $from_email comes from WordPress, this is the default $from_name 
     // you must modify the $from_email from within this function before returning it 
     return $from_email; 
    } 
} 
+0

它應該是'&$ this',而不是'$ this'。 – Dogbert 2011-04-11 23:17:16

+0

這是一個技術性嗎?在我編寫的插件中,它的工作原理沒有作爲參考傳遞。 – radiok 2011-04-11 23:20:57

+0

抱歉,直言不諱:) WordPress API提及使用&$ var無處不在。我剛剛在PHP手冊中閱讀了更多。它看起來像'&$ var'被用於PHP 4兼容性(以確保變量在PHP 4中通過引用傳遞)。這在PHP5中不需​​要。 – Dogbert 2011-04-11 23:52:25

相關問題