【问题标题】:Wordpress Filter, changing plugin ArrayWordpress 过滤器,更改插件数组
【发布时间】:2020-04-06 02:06:59
【问题描述】:

这是我第一次尝试向我的functions.php 添加过滤器。我正在尝试覆盖插件设置的表单设置,具体取决于使用的表单或查看的页面。

我试图覆盖的数组在插件中看起来像这样:

$form_settings = (object) array(
            'form_title'                    => stripslashes( html_entity_decode( $form->form_title, ENT_QUOTES, 'UTF-8' ) ),
            'form_subject'                  => stripslashes( html_entity_decode( $form->form_email_subject, ENT_QUOTES, 'UTF-8' ) ),
            'form_to'                       => ( is_array( unserialize( $form->form_email_to ) ) ) ? unserialize( $form->form_email_to ) : explode( ',', unserialize( $form->form_email_to ) ),
            'form_from'                     => stripslashes( $form->form_email_from ),
            'form_from_name'                => stripslashes( $form->form_email_from_name ),
            'form_notification_setting'     => stripslashes( $form->form_notification_setting ),
            'form_notification_email_name'  => stripslashes( $form->form_notification_email_name ),
            'form_notification_email_from'  => stripslashes( $form->form_notification_email_from ),
            'form_notification_subject'     => stripslashes( html_entity_decode( $form->form_notification_subject, ENT_QUOTES, 'UTF-8' ) ),
            'form_notification_message'     => stripslashes( $form->form_notification_message ),
            'form_notification_entry'       => stripslashes( $form->form_notification_entry )
        );
        // Allow the form settings to be filtered (ex: return $form_settings->'form_title' = 'Hello World';)
        $form_settings = (object) apply_filters_ref_array( 'vfb_email_form_settings', array( $form_settings, $form_id ) );

我的functions.php看起来像

function maildeponselectcountry() {
    //here comes the new mailadress 
    $form_id = 2;

    $form_settings = array(
        "form_title" => "test"
    );

    apply_filters_ref_array( 'vfb_email_form_settings', array( $form_settings, $form_id ) );

}

apply_filters( 'vfb_email_form_settings', 'maildeponselectcountry' );

我对 apply_filters_ref_array 感到困惑。我该怎么办?我真的只想覆盖 $form_settings 数组中的 form_to。我会很感激任何提示!非常感谢!

【问题讨论】:

    标签: wordpress forms filter


    【解决方案1】:

    您不需要使用apply_filters_ref_array。这就是插件允许您使用过滤器的地方。您还想使用add_filter 而不是apply_filterapply_filter 用于设置允许过滤的数据。

    您的函数应该如下所示:

    function maildeponselectcountry( array( $form_settings, $form_id ) ) {
    
        /* not sure what is actually passed here, 
           but if you need to check for the form_id, 
           it might be something like this. The passed array is actually an object 
           because of typecasting. */
    
        if ( $form_id->id == 2 ) {
           // Set the object property value
           $form_settings->form_title = 'test';
        }
    
        // return the form_settings with or without the updated value (depending on the form_id)
        return $form_settings;
    }
    
    // Use add_filter and set the priority.
    add_filter( 'vfb_email_form_settings', 'maildeponselectcountry', 10 );
    

    编辑:

    尝试运行此测试:

    function maildeponselectcountry( $form_settings ) {
    
           return $form_settings->form_title = 'Test';
        }
    
        // Use add_filter and set the priority.
        add_filter( 'vfb_email_form_settings', 'maildeponselectcountry', 10 );
    

    运行时,从 '' 发布输出,以便我们可以看到返回的数据。

    【讨论】:

    • 哦哇非常感谢!这部分似乎有问题`function maildeponselectcountry(array($form_settings,$form_id)){`它会创建一个服务器错误,它说页面有问题。当我把阵列拿走时,网页又可以工作了。
    • 错误是什么?此外,您也许可以只传递$form_settings 而不是array($form_settings, $form_id)。我没有你的确切插件,所以测试有点困难。
    • 是的,我只是尝试了 function maildeponselectcountry( $form_settings ) { 这是有效的。但它不会覆盖数组。该插件是 Visual Form Builder 免费版
    • 我确实运行了你的测试。它没有给出错误,但我如何查看它是否添加了
       我在前端看不到它。
    • 测试回显是这样的:'stdClass Object ( [form_title] => Kontaktformular [form_subject] => Kontaktformular ... [form_to] => Array ( [0] => test@test.ch ) [form_from] => test@test.ch [form_from_name] => ORELL Tec [form_notification_setting] => [form_notification_email_name] => [form_notification_email_from] => [form_notification_subject] => [form_notification_message] => [form_notification_entry] => )'
    猜你喜欢
    • 1970-01-01
    • 2019-01-01
    • 2016-08-16
    • 2015-04-12
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    相关资源
    最近更新 更多