0

I use code that displays custom WooCommerce product tabs. This code is based on "Custom metabox content displayed in single product additional tabs on Woocommerce"

// Add a custom metabox
add_action( 'add_meta_boxes', 'additional_product_tabs_metabox' );
function additional_product_tabs_metabox()
{
    add_meta_box(
        'add_product_metabox_additional_tabs',
        __( 'Specifications Product Tabs', 'woocommerce' ),
        'additional_product_tabs_metabox_content',
        'product',
        'normal'        
    );
}

//  Add custom metabox content
function additional_product_tabs_metabox_content( $post )
{
    // Technical Specification
    echo '<h4>' . __( 'Technical Specification', 'woocommerce' ) . '</h4>';
    $value = get_post_meta( $post->ID, '_technical_specification', true );
    wp_editor( $value, '_technical_specification', array( 'editor_height' => 150 ) );

    // Nonce field (for security)
    echo '<input type="hidden" name="additional_product_tabs_nonce" value="' . wp_create_nonce() . '">';
}


// Save product data
add_action( 'save_post_product', 'save_additional_product_tabs', 10, 1 );
function save_additional_product_tabs( $post_id ) {

    // Security check
    if ( ! isset( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
        return $post_id;
    }

    //Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
        return $post_id;
    }

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    if ( ! current_user_can( 'edit_product', $post_id ) ) {
        return $post_id;
    }

    // Sanitize user input and save the post meta fields values.

    if( isset($_POST[ '_technical_specification' ]) )
        update_post_meta( $post_id, '_technical_specification', wp_kses_post($_POST[ '_technical_specification' ]) );

}

add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {   
    global $product;
    
    // Technical Specification Tab
    if ( ! empty($product) ) {
        $tabs['technical_specification_tab'] = array(
            'title'     => __( 'Technical Specification', 'woocommerce' ),
            'priority'  => 60,
            'callback'  => 'woo_technical_specification_tab_content'
        );
    }
    return $tabs;
}

function woo_technical_specification_tab_content()  {
    global $product;

    echo'<p class="specification-text">'. $product->get_meta( '_technical_specification' ) . '</p>';
}

add_action('wp_footer', 'woo_activate_pdf_script');
function woo_activate_pdf_script(){
?>
<script>
    
   var i, links = document.getElementsByTagName('a');
    
    for(i=0; i<links.length; i++) {
        if (links[i].href.match(/.pdf/ig)) links[i].className = 'pdf-icon';
    }
    
</script>
<?php
}

Can you also check this code to see if it is correct? Tab visibility doesn't work here if there's no content in it.

And the speed of saving the product page when editing has increased.

I would be glad to have your help!

Dmitry
  • 119
  • 1
  • 9
  • 38

0 Answers0