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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.