【发布时间】:2015-04-12 17:34:21
【问题描述】:
我已经安装了一个 Wordpress 插件(Profile Builder),其中包含用于过滤功能的说明:
Filter 'wppb_signup_user_notification_filter' to bypass this function or
replace it with your own notification behavior.
确实,我想用我自己的变体替换遵循此指令的函数,该变体会更改正在发送的电子邮件的文本。原来的函数是这样开始的:
function wppb_signup_user_notification( $user, $user_email, $activation_key, $meta = '' ) {
if ( !apply_filters( 'wppb_signup_user_notification_filter', $user, $user_email, $activation_key, $meta ) )
return false;
...
}
我在自己的插件中复制了该功能并将其重命名:
function cvc_signup_user_notification( $user, $user_email, $activation_key, $meta = '' ) {
...(NOTE: I left out the if(!apply_filters conditional as it caused a server 500 error)
}
我现在在这个过程的 remove_filter 和 add_filter 部分遇到了困难。我试过了:
remove_filter( 'wppb_signup_user_notification_filter', 'wppd_signup_user_notification');
add_filter( 'wppb_signup_user_notification_filter', 'cvc_signup_user_notification');
有了这个结果:
Warning: Missing argument 2 for cvc_signup_user_notification() in /home/ccarlv5/public_html/homebaseinc/wp-content/plugins/cvc-profile-builder-homebase-emails/index.php on line 306
Warning: Missing argument 3 for cvc_signup_user_notification() in /home/ccarlv5/public_html/homebaseinc/wp-content/plugins/cvc-profile-builder-homebase-emails/index.php on line 306
而这次尝试:
remove_filter( 'wppb_signup_user_notification_filter', 'wppd_signup_user_notification',$user, $user_email, $activation_key, $meta);
add_filter( 'wppb_signup_user_notification_filter', 'cvc_signup_user_notification',$user, $user_email, $activation_key, $meta);
产生这个结果:
Warning: Missing argument 1 for cvc_signup_user_notification() in /home/ccarlv5/public_html/homebaseinc/wp-content/plugins/cvc-profile-builder-homebase-emails/index.php on line 306
Warning: Missing argument 2 for cvc_signup_user_notification() in /home/ccarlv5/public_html/homebaseinc/wp-content/plugins/cvc-profile-builder-homebase-emails/index.php on line 306
Warning: Missing argument 3 for cvc_signup_user_notification() in /home/ccarlv5/public_html/homebaseinc/wp-content/plugins/cvc-profile-builder-homebase-emails/index.php on line 306
我尝试在不删除原始过滤器的情况下添加过滤器,这会导致相同的 3 个错误(并且显然仍在使用原始功能)
有人可以指出正确的方向,以实现用我自己的功能替换原始功能并修改电子邮件通知的目标。
任何指导将不胜感激。 谢谢。
【问题讨论】:
-
我找到了答案。我需要明确指定 add_filter 的优先级和参数数量:
add_filter( 'wppb_signup_user_notification_filter', 'cvc_signup_user_notification',10,4); -
我说得太早了。尽管我的新函数正在运行,但没有发生缺少参数的错误,并且我收到了带有自定义文本的电子邮件,但我还收到了来自原始函数的重复电子邮件。所以 remove_filter 行不工作。有什么建议吗?