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?

Related posts

4 comments

  1. The woocommerce_product_tabs filter is using call_user_func function to process the callback:

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

    So actually it is sending two parameters to the callback: the key (so in your case finance_tab) and the whole tab array. So, theoretically, you should be able to do this:

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

    Then:

    function woo_finance_tab_content($tab_name, $tab) {
        echo $tab['callback_parameters']; // display "stuff"
    }
    
  2. Unfortunately not.

    The arguments available in a callback like this will only be the arguments defined by the callback.

    See if the data you want is available in the arguments passed to the callback already, as they often will be.

  3. 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!

  4. I was able to pass an array as argument like this:

    function show_sizechart_tab( $tabs ) {
        global $post;
    
        $sizechart_image_ids = get_post_meta( $post->ID, 'sizechart_image_ids' );
        if ( count($sizechart_image_ids)>0 ){
            $tabs['sizechart_tab'] = array(
                'title'    => __( 'Size chart', 'woocommerce' ),
                'priority' => 10,
                'callback' => 'sizechart_product_tab_content',
                'image_ids'=> $sizechart_image_ids
            );
        }
        return $tabs;
    }
    function sizechart_product_tab_content($name, $tab_arr) {
    print_r("<pre>");
    print_r($tab_arr);
    print_r("</pre>");
    
        echo '<h2>New Product Tab</h2>';
        echo '<p>Here's your new product tab.</p>';
    
    }
    

    the tab_arr will have image_ids

Comments are closed.