Skip to content

Instantly share code, notes, and snippets.

@ashwebstudio
Created February 25, 2016 09:39
Show Gist options
  • Save ashwebstudio/65dd84ee5246c5f5a467 to your computer and use it in GitHub Desktop.
Save ashwebstudio/65dd84ee5246c5f5a467 to your computer and use it in GitHub Desktop.
WooCommerce tabs using Advanced Custom Fields
// First create a field group assigned to the Product custom post type
// Make a repeater field with sub-fields "Title" (text) and "Content" (WYSIWYG)
add_filter( 'woocommerce_product_tabs', 'custom_tabs' );
function custom_tabs( $tabs ) {
global $post;
$priority = 100;
while ( has_sub_field( 'tabs', $post->ID ) ) {
$title = get_sub_field( 'title' );
$content = get_sub_field( 'content' );
$id = sanitize_title( $title );
$id = str_replace( '-', '_', $id );
$tabs[ $id ] = array(
'title' => $title,
'priority' => $priority++,
'callback' => 'custom_tab_content'
);
}
return $tabs;
}
function custom_tab_content( $id ) {
global $post;
while ( has_sub_field( 'tabs', $post->ID ) ) {
$title = get_sub_field( 'title' );
$this_id = sanitize_title( $title );
$this_ = str_replace( '-', '_', $this_ );
if ( $id == $this_id ) {
the_sub_field( 'content' );
}
}
}
@joelalexandersson
Copy link

Just if anyone had a problem with this script. On line 31:
$this_ = str_replace( '-', '', $this ); is missing the id at the end:
$this_id = str_replace( '-', '_', $this_id );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment