Add something after a filter

时间:2018-07-25 作者:Quentin

我正在使用wp众筹插件,并尝试在过滤器中添加一些内容。这里是原始过滤器:

 apply_filters(\'wpneo_crowdfunding_frontend_dashboard_menus\', array(
        \'dashboard\' =>
            array(
                \'tab\'             => \'dashboard\',
                \'tab_name\'        => __(\'Dashboard\',\'wp-crowdfunding\'),
                \'load_form_file\'  => WPNEO_CROWDFUNDING_DIR_PATH.\'includes/woocommerce/dashboard/dashboard.php\'
            ),
        \'profile\' =>
            array(
                \'tab\'             => \'account\',
                \'tab_name\'        => __(\'Profile\',\'wp-crowdfunding\'),
                \'load_form_file\'  => WPNEO_CROWDFUNDING_DIR_PATH.\'includes/woocommerce/dashboard/profile.php\'
            ),
        \'contact\' =>
            array(
                \'tab\'             => \'account\',
                \'tab_name\'        => __(\'Contact\',\'wp-crowdfunding\'),
                \'load_form_file\'  => WPNEO_CROWDFUNDING_DIR_PATH.\'includes/woocommerce/dashboard/contact.php\'
            ),
        \'campaign\' =>
            array(
                \'tab\'             => \'campaign\',
                \'tab_name\'        => __(\'My Campaigns\',\'wp-crowdfunding\'),
                \'load_form_file\'  => WPNEO_CROWDFUNDING_DIR_PATH.\'includes/woocommerce/dashboard/campaign.php\'
            ),
        \'backed_campaigns\' =>
            array(
                \'tab\'             => \'campaign\',
                \'tab_name\'        => __(\'Backed Campaigns\',\'wp-crowdfunding\'),
                \'load_form_file\'  => WPNEO_CROWDFUNDING_DIR_PATH.\'includes/woocommerce/dashboard/investment.php\'
            ),
        \'pledges_received\' =>
            array(
                \'tab\'            => \'campaign\',
                \'tab_name\'       => __(\'Pledges Received\',\'wp-crowdfunding\'),
                \'load_form_file\' => WPNEO_CROWDFUNDING_DIR_PATH.\'includes/woocommerce/dashboard/order.php\'
            ),
        \'bookmark\' =>
            array(
                \'tab\'            => \'campaign\',
                \'tab_name\'       => __(\'Bookmarks\',\'wp-crowdfunding\'),
                \'load_form_file\' => WPNEO_CROWDFUNDING_DIR_PATH.\'includes/woocommerce/dashboard/bookmark.php\'
            ),
        \'password\' =>
            array(
                \'tab\'            => \'account\',
                \'tab_name\'       => __(\'Password\',\'wp-crowdfunding\'),
                \'load_form_file\' => WPNEO_CROWDFUNDING_DIR_PATH.\'includes/woocommerce/dashboard/password.php\'
            ),
        \'rewards\' =>
            array(
                \'tab\'            => \'account\',
                \'tab_name\'       => __(\'Rewards\',\'wp-crowdfunding\'),
                \'load_form_file\' => WPNEO_CROWDFUNDING_DIR_PATH.\'includes/woocommerce/dashboard/rewards.php\'
            ),
    ));
我需要的是在末尾添加以下内容,我想是相同的过滤器:

    \'myform\' =>
        array(
            \'tab\'            => \'myform\',
            \'tab_name\'       => __(\'My Form\'),
            \'load_form_file\' => MY_THEME_DIR.\'wpcrowdfunding/dashboard/myform.php\'
        ),
有可能吗?

1 个回复
最合适的回答,由SO网友:Levi Dulstein 整理而成

这正是过滤器的作用!apply_filters 是一个挂钩,允许您修改作为第二个参数传递到那里的数组。尝试以下操作:

function example_additional_wpneo_menus( $menus ) {

    // check the type just in case
    if ( ! is_array( $menus ) ) {
        return $menus;
    }

    $menus[\'myform\'] = [
        \'tab\'            => \'myform\',
        // here the second arg is a name of your theme/plugin textdomain, used for translations only
        \'tab_name\'       => __(\'My Form\',\'your_textodomain\'),
        // MY_THEME_DIR doesn\'t seem to be a wordpress constant, try this if you want to get current theme directory
        // https://developer.wordpress.org/reference/functions/get_template_directory/
        \'load_form_file\' => get_template_directory() . \'/wpcrowdfunding/dashboard/myform.php\'
    ];

    return $menus;
}

// now you hook that into the filter
add_filter( \'wpneo_crowdfunding_frontend_dashboard_menus\', \'example_additional_wpneo_menus\', 10, 1 );

结束