Liam Liam - 5 years ago 165
PHP Question

Passing a argument using callback key in wordpress

I'm working in WordPress here. I have the following code:

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;
}


This works as you'd expect, and calls the
$woo_finance_tab_content
. However, I'd like to pass some arguments to
$woo_finance_tab_content
tab. Is this possible with this scenario?

Answer Source

Ok so this is possible (it's a little hacky).

As @vard suggested declare the tab like this:

public function woo_finance_tab( $tabs ) {      
        if (is_product() && is_woocommerce() && $this->category_has_finance() == true)  {   
            $tabs['finance_tab'] = array(
                'title'     => __( 'Finance Options', 'woocommerce' ),
                'priority'  => 50,
                'callback'  => array ($this, 'woo_finance_tab_content' ),
                'callback_parameters' => array ($this, 'stuff')
                );
                return $tabs;
        }

Now in the tab function (I.E the function defined in callback), do the following:

public function woo_finance_tab_content($name,$tab_arr) {
     var_dump($tab_arr["callback_parameters"]["1"]);
}

The var dump will display "stuff". Clearly useless here, but this could also be an extremely useful array. Hopefully this will come in useful to someone!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download