在wordpress中使用回调键传递参数

Lia*_*ell 2 php wordpress

我在这里工作在WordPress.我有以下代码:

if (is_product() && is_woocommerce() && $this->category_has_fiance() == true) { 
            $tabs['finance_tab'] = array(
                'title'     => __( 'Finance Options', 'woocommerce' ),
                'priority'  => 50,
                'callback'  => array ($this, 'woo_finance_tab_content')
                );
                return $tabs;
        }
Run Code Online (Sandbox Code Playgroud)

这可以按照您的预期工作,并调用$woo_finance_tab_content.但是,我想将一些参数传递给$woo_finance_tab_contenttab.这种情况可能吗?

var*_*ard 5

woocommerce_product_tabs滤波器是用call_user_func函数来处理回调:

<?php foreach ( $tabs as $key => $tab ) : ?>
    <div class="panel entry-content wc-tab" id="tab-<?php echo esc_attr( $key ); ?>">
        <?php call_user_func( $tab['callback'], $key, $tab ); ?>
    </div>
<?php endforeach; ?>
Run Code Online (Sandbox Code Playgroud)

所以它实际上是向回调发送两个参数:键(在你的情况下finance_tab)和整个tab数组.所以,从理论上讲,你应该能够做到这一点:

$tabs['finance_tab'] = array(
    'title'     => __( 'Finance Options', 'woocommerce' ),
    'priority'  => 50,
    'callback'  => array ($this, 'woo_finance_tab_content'),
    'callback_parameters' => 'stuff'
);
Run Code Online (Sandbox Code Playgroud)

然后:

function woo_finance_tab_content($tab_name, $tab) {
    echo $tab['callback_parameters']; // display "stuff"
}
Run Code Online (Sandbox Code Playgroud)