您可以使用apply_filters
功能:
function koku_crm_send_sendgrid($sendgrid_api_key, $to, $subject, $text, $html) {
$to = apply_filters( \'koku_crm_send_to\', $to );
$sendgrid = new \\SendGrid($sendgrid_api_key);
$mail = new KCSendGrid\\Mail();
$from = new KCSendGrid\\Email(get_bloginfo( \'name\' ), get_bloginfo( \'admin_email\' ));
$mail->setFrom($from);
$mail->setSubject(html_entity_decode($subject, ENT_QUOTES, \'UTF-8\'));
$content = new KCSendGrid\\Content("text/plain", $text);
$mail->addContent($content);
$content = new KCSendGrid\\Content("text/html", $html);
$mail->addContent($content);
$personalization = new KCSendGrid\\Personalization();
$to = new KCSendGrid\\Email(null, $to);
$personalization->addTo($to);
$mail->addPersonalization($personalization);
$sendgrid->client->mail()->send()->post($mail);
}
的第一个参数
apply_filters()
是筛选器的名称。这是你打电话时要用到的
add_filter()
. 第二个参数是要过滤的值。
现在您可以筛选$to
像这样:
function wpse_276933_send_to( $to ) {
$to = \'[email protected]\';
return $to;
}
add_filter( \'koku_crm_send_to\', \'wpse_276933_send_to\' );
通过将参数添加到,可以将更多值传递到过滤器回调函数中
apply_filters()
. 它们自己不会做任何事情,但这意味着它们在添加过滤器时可用。因此,如果您的过滤器是:
$to = apply_filters( \'koku_crm_send_to\', $to, $subject, $text );
您可以访问
$subject
和
$text
通过在回调函数中包含参数并设置
add_filter()
到
3
, 使函数知道接受3个参数:
function wpse_276933_send_to( $to, $subject, $text ) {
if ( $subject === \'Subject One\' ) {
$to = \'[email protected]\';
}
return $to;
}
add_filter( \'koku_crm_send_to\', \'wpse_276933_send_to\', 10, 3 );
阅读插件手册中的更多内容:
https://developer.wordpress.org/plugins/hooks/custom-hooks/