【问题标题】:replace Wordpress plugin function using filter使用过滤器替换 Wordpress 插件功能
【发布时间】: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 行不工作。有什么建议吗?

标签: wordpress function filter


【解决方案1】:

试试这个:我添加了带有参数的add_filter()函数..我没有对插件文件进行任何更改,谢谢

function my_function_name($user, $user_email, $activation_key, $meta = '' ){


$wppb_general_settings = get_option( 'wppb_general_settings' ); 
$admin_email = get_site_option( 'admin_email' );

if ( $admin_email == '' )
    $admin_email = 'support@' . $_SERVER['SERVER_NAME'];

$from_name = apply_filters ( 'wppb_signup_user_notification_email_from_field', get_bloginfo( 'name' ) );

$message_headers = apply_filters ( 'wppb_signup_user_notification_from', "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n" );

$registration_page_url = ( ( isset( $wppb_general_settings['activationLandingPage'] ) && ( trim( $wppb_general_settings['activationLandingPage'] ) != '' ) ) ? add_query_arg( array('activation_key' => $activation_key ), get_permalink( $wppb_general_settings['activationLandingPage'] ) ) : 'not_set' );    
if ( $registration_page_url == 'not_set' ){
    global $post;
    if( !empty( $post->ID ) )
        $permalink = get_permalink( $post->ID );
    else
        $permalink =  get_bloginfo( 'url' );

    if( !empty( $post->post_content ) )
        $post_content = $post->post_content;
    else
        $post_content = '';

    $registration_page_url = ( ( strpos( $post_content, '[wppb-register' ) !== false ) ? add_query_arg( array('activation_key' => $activation_key ), $permalink ) : add_query_arg( array('activation_key' => $activation_key ), get_bloginfo( 'url' ) ) );
}

$subject = sprintf( __( '[%1$s] Activate %2$s', 'profilebuilder'), $from_name, $user );
$subject = apply_filters( 'wppb_signup_user_notification_email_subject', $subject, $user_email, $user, $activation_key, $registration_page_url, $meta, $from_name, 'wppb_user_emailc_registr_w_email_confirm_email_subject' );

$message = sprintf( __( "To activate your user, please click the following link:\n\n%s%s%s\n\nAfter you activate it you will receive yet *another email* with your login.", "profilebuilder" ), '<a href="'.$registration_page_url.'">', $registration_page_url, '</a>.' );
$message = apply_filters( 'wppb_signup_user_notification_email_content', $message, $user_email, $user, $activation_key, $registration_page_url, $meta, $from_name, 'wppb_user_emailc_registr_w_email_confirm_email_content' );

wppb_mail( $user_email, $subject, $message, $from_name, '', $user, '', $user_email, 'register_w_email_confirmation', $registration_page_url, $meta );

return true;

}
add_filter('wppb_signup_user_notification_filter','my_function_name', 10);

【讨论】:

  • 感谢您的努力。您所做的和我所做的之间的区别似乎是 a) 您添加了 ',10' 优先级,并且您将 add_filter 放置在函数之后而不是之前。我按照您的方式尝试过,但结果仍然是缺少参数 2 和 3 的警告。此外,该功能确实发送了电子邮件,但它不包括我的功能中的任何文本更改。它仍在使用原来的功能。
  • 实际上,您帮助我指出了正确的方向以找到答案。请参阅我在原始问题中的评论。
猜你喜欢
  • 2016-08-16
  • 1970-01-01
  • 2015-02-13
  • 2017-09-11
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
相关资源
最近更新 更多