0

I am creating a plugin to handle woocommerce product tabs.

I load the new custom tabs by hooking into woocommerce_product_tabs

add_filter( 'woocommerce_product_tabs', 'benz_new_product_tabs' );

I then have a function to load the tab titles based on a DB meta_value - everything is working fine. -- I have deleted most of the stuff to spare you - but tried to keep everything that is important, relevant.

function benz_new_product_tabs( $tabs ) {
  global $post;
  $benz_tab_count = get_post_meta( $post->ID, '_tabs_total_number', true );
  for ( $x = 0; $x < $benz_tab_count; $x++ ) {
    $y=$x+1;
    $benz_tab_title = get_post_meta( $post->ID, "_tabs_title_$y", true );
    $benz_tab_title_clean = preg_replace('/\s+/', '-', $benz_tab_title);

    if ( strlen($benz_tab_title) > 0  ) {
      $tabs[$benz_tab_title_clean] = array(
        'title'     => __( $benz_tab_title, 'woocommerce' ),
        'priority'  => $y+50,
        'callback'  => 'benz_new_product_tab_content'.$y
      );
    } // end foreach
  }
  return $tabs;
}

So - this generates as many tab titles as needed per db table and assigns a unique callback function to then provide content for each tab.

The call back functions are what I am struggling with. I would like to create a loop that will generate each callback function and the relevant content.

The following functions are working fine - but i would really like to make this smarter, if possible.

function benz_new_product_tab_content1() {
  global $post;
   $benz_tab_content = get_post_meta( $post->ID, "_tabs_content_1", true );
   if (strlen($benz_tab_content) > 0) {
     echo $benz_tab_content;
   }
}

function benz_new_product_tab_content2() {
  global $post;
   $benz_tab_content = get_post_meta( $post->ID, "_tabs_content_2", true );
   if (strlen($benz_tab_content) > 0) {
     echo $benz_tab_content;
   }
}

function benz_new_product_tab_content3() {
  global $post;
   $benz_tab_content = get_post_meta( $post->ID, "_tabs_content_3", true );
   if (strlen($benz_tab_content) > 0) {
     echo $benz_tab_content;
   }
}

I have more than 3 tabs on many of my products, sometimes as much as 8 ( per client ) so I have that block of call back functions pasted 8 times in my plugin file. I have tried to put it in a for loop - but obviously, you cant concatenate a variable onto the end of a function name.

Let me know your thoughts, thanks for reading.

1

Probably you can do it with args:

'callback' => benz_new_product_tab_content,
'args' => '_tabs_content_' . $y

And your function:

function benz_new_product_tab_content($param, $args) { 
  global $post; 
  $table = end($args); 
  $benz_tab_content = get_post_meta( $post->ID, $table, true ); 

  if (strlen($benz_tab_content) > 0) { 
    echo $benz_tab_content; 
  } 
}

One alternative is to create anonymous function like this:

  'callback' => function() { // do your work }

Typically to create functions in FOR loop you would use eval. I think this is not the smart choice.

| improve this answer | |
  • @RobBenz edit done. – prosti Jan 12 '17 at 13:41
  • No problem. You may edit this code further if you have something to add. – prosti Jan 12 '17 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.