Halloween Sale 15% off Ends in See the plans
PHP

Downloads Tab With Manuals and Data Sheets

Adds a "Documents" field to products for links to manuals, data sheets or certificates, listed in a Downloads tab on the product page.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'woocommerce_product_options_general_product_data', function () {
    global $product_object;

    woocommerce_wp_textarea_input( array(
        'id'          => '_wpide_documents',
        'label'       => 'Documents',
        'placeholder' => "User manual | https://example.com/manual.pdf\nData sheet | https://example.com/sheet.pdf",
        'description' => 'One per line: Label | URL',
        'desc_tip'    => true,
        'value'       => $product_object instanceof WC_Product ? $product_object->get_meta( '_wpide_documents' ) : '',
    ) );
} );

add_action( 'woocommerce_admin_process_product_object', function ( $product ) {
    // phpcs:ignore WordPress.Security.NonceVerification -- WooCommerce verifies the product save nonce.
    if ( isset( $_POST['_wpide_documents'] ) ) {
        $product->update_meta_data( '_wpide_documents', sanitize_textarea_field( wp_unslash( $_POST['_wpide_documents'] ) ) );
    }
} );

add_filter( 'woocommerce_product_tabs', function ( $tabs ) {
    global $product;

    $raw  = $product instanceof WC_Product ? (string) $product->get_meta( '_wpide_documents' ) : '';
    $docs = array();

    foreach ( preg_split( '/\r\n|\n/', $raw ) as $line ) {
        $parts = array_map( 'trim', explode( '|', $line, 2 ) );

        if ( 2 === count( $parts ) && wp_http_validate_url( $parts[1] ) ) {
            $docs[] = $parts;
        }
    }

    if ( $docs ) {
        $tabs['wpide_documents'] = array(
            'title'    => 'Downloads',
            'priority' => 35,
            'callback' => function () use ( $docs ) {
                echo '<ul class="wpide-documents">';

                foreach ( $docs as $doc ) {
                    printf( '<li><a href="%s" target="_blank" rel="noopener">%s</a></li>', esc_url( $doc[1] ), esc_html( $doc[0] ) );
                }

                echo '</ul>';
            },
        );
    }

    return $tabs;
} );

Notes

Enter one document per line as "Label | URL", using URLs of PDFs uploaded to the media library.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.