有几种可能性。
Use the settings API to add a form field somewhere in your admin area 用户可以在其中输入他们想要使用的电子邮件。使用在挂钩函数中检索它get_option
.
如果你想在任何地方都使用同样的电子邮件,这将是一种方法。
<?php
add_filter(\'wp_mail_from\', \'wpse66067_mail_from\');
function wpse66067_mail_from($email)
{
if($e = get_option(\'wpse66067_option\'))
return $e;
return $email; // we don\'t have anything in the option return default.
}
Use an object, and store the from email as a property. 如果您需要在每次发送的基础上更改发件人电子邮件,这将是一种方法。
<?php
class WPSE66067_Emailer
{
private static $from_email = \'[email protected]\';
public static function send_mail()
{
add_filter(\'wp_mail_from\', array(__CLASS__, \'from_email\');
// change the email
self::$from_email = \'[email protected]\';
wp_mail( /* args here */ );
// you probably don\'t need to do this.
remove_filter(\'wp_mail_from\', array(__CLASS__, \'from_email\');
}
public static function from_email($e)
{
if(self::$from_email)
return self::$from_email;
return $e;
}
}
Use a global. 这(可能)是个糟糕的主意。如果每次都需要更改电子邮件,请使用对象和属性(请参见上文)。
<?php
$wpse66067_email = \'[email protected]\';
function wpse66067_send_mail()
{
// globalize and change the email
global $wpse66067_email;
$wpse66067_email = \'[email protected]\';
add_filter(\'wp_mail_from\', \'wpse66067_from_email\');
wp_mail( /* args here */ );
}
function wpse66067_from_email($e)
{
global $wpse66067_email;
if($wpse66067_email)
return $wpse66067_email;
return $e;
}