Skip to content

Instantly share code, notes, and snippets.

@smeric
Last active January 21, 2019 11:04
Show Gist options
  • Save smeric/ff09b2f993dc8378a04a to your computer and use it in GitHub Desktop.
Save smeric/ff09b2f993dc8378a04a to your computer and use it in GitHub Desktop.
Adds a metabox to define a custom tab title and content on woocommerce product pages.
<?php
/**
* Woocommerce custom extra tab
*
* Adds a metabox to define a custom tab label and content on woocommerce product pages. Also remove predifined
* description tab title so we can define our own directly inside tab content.
*
* @link https://gist.github.com/smeric/ff09b2f993dc8378a04a
* @version 1.0.1
* @package woocommerce_custom_extra_tab
* @credits Brad Dalton
* @see http://wpsites.net/wordpress-admin/custom-meta-box-functions-code/
*
* @wordpress-plugin
* Plugin Name: Woocommerce custom extra tab
* Plugin URI: https://gist.github.com/smeric/ff09b2f993dc8378a04a
* Description: Adds a metabox to define a custom tab label and content on woocommerce product pages. Also remove predifined description tab title so we can define our own directly inside tab content.
* Version: 1.0.1
* Author: Sébastien Méric <[email protected]>
* Author URI: http://www.sebastien-meric.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: woocommerce_custom_extra_tab
* Domain Path: /languages
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Define the internationalization functionality.
*
* Loads and defines the internationalization files for this plugin
* so that its ready for translation.
*
* @since 1.0.0
* @package woocommerce_custom_extra_tab
* @author Sébastien Méric <[email protected]>
**/
add_action( 'after_setup_theme', 'woocommerce_custom_extra_tab_after_setup_theme' );
function woocommerce_custom_extra_tab_after_setup_theme() {
// Load the plugin text domain for translation.
$locale = apply_filters( 'woocommerce_custom_extra_tab_locale', get_locale(), 'woocommerce_custom_extra_tab' );
load_textdomain( 'woocommerce_custom_extra_tab', trailingslashit( WP_LANG_DIR ) . 'woocommerce_custom_extra_tab/woocommerce_custom_extra_tab-' . $locale . '.mo' );
load_plugin_textdomain( 'woocommerce_custom_extra_tab', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Remove default description tab title so we may specify a custom one directly inside content.
*
* @since 1.0.1
* @package woocommerce_custom_extra_tab
* @author Sébastien Méric <[email protected]>
*/
add_filter( 'woocommerce_product_description_heading', '__return_empty_string', 100 );
/**
* Add the new tab to product page.
*
* @since 1.0.0
* @package woocommerce_custom_extra_tab
* @author Sébastien Méric <[email protected]>
*/
add_filter( 'woocommerce_product_tabs', 'woocommerce_custom_extra_tab' );
function woocommerce_custom_extra_tab( $tabs ) {
global $post;
$custom_tab_label = get_post_meta( $post->ID, 'custom_tab_label', true );
if ( $custom_tab_label ) {
// Adds the new tab
$tabs['custom_tab'] = array(
'title' => $custom_tab_label,
'priority' => 29,
'callback' => 'woocommerce_custom_extra_tab_content'
);
}
return $tabs;
}
/**
* Display custom tab content.
*
* @since 1.0.0
* @package woocommerce_custom_extra_tab
* @author Sébastien Méric <[email protected]>
**/
function woocommerce_custom_extra_tab_content() {
global $post;
$custom_tab_content = get_post_meta( $post->ID, 'custom_tab_content', true );
// The new tab formated content with shortcodes support
echo wpautop( do_shortcode( $custom_tab_content ) );
}
/**
* Meta box parameters.
*
* @since 1.0.0
* @package woocommerce_custom_extra_tab
* @author Sébastien Méric <[email protected]>
*/
add_action( 'add_meta_boxes', 'woocommerce_custom_extra_tab_register_metabox' );
function woocommerce_custom_extra_tab_register_metabox() {
add_meta_box(
'custom_tab_meta_box',
__( 'Custom tab', 'woocommerce_custom_extra_tab' ),
'woocommerce_custom_extra_tab_meta_box',
'product',
'normal',
'low'
);
}
/**
* Create nonce, output tab label field and mini editor on edit product screen.
*
* @since 1.0.0
* @package woocommerce_custom_extra_tab
* @author Sébastien Méric <[email protected]>
*/
function woocommerce_custom_extra_tab_meta_box( $post ) {
$nonce = wp_create_nonce( 'woocommerce_custom_extra_tab_meta_box_nonce' );
$value = get_post_meta( $post->ID, 'custom_tab_label', true );
?>
<input type="hidden" name="woocommerce_custom_extra_tab_meta_box_nonce" value="<?php echo $nonce ?>" />
<p>
<label for="custom_tab_label"><?php _e( 'Tab label', 'woocommerce_custom_extra_tab' ) ?></label><br />
<input type="text" name="custom_tab_label" id="custom_tab_label" value="<?php echo( $value ? $value : '' ) ?>" class="regular-text" />
</p>
<p>
<label for="custom_tab_content"><?php _e( 'Tab content', 'woocommerce_custom_extra_tab' ) ?></label>
</p>
<?php
$value = get_post_meta( $post->ID, 'custom_tab_content', true );
wp_editor( $value, 'custom_tab_content', array( 'textarea_rows' => 10 ) );
}
/**
* Run checks before saving tab label and content to database.
*
* @since 1.0.0
* @package woocommerce_custom_extra_tab
* @author Sébastien Méric <[email protected]>
*/
add_action( 'save_post', 'woocommerce_custom_extra_tab_save_meta_box_content', 10, 2 );
function woocommerce_custom_extra_tab_save_meta_box_content( $post_id ) {
// If this is an autosave, our form has not been submitted, don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE
|| ! isset ( $_POST['post_type'] )
|| 'product' !== $_POST['post_type']
|| ! current_user_can( 'edit_products', $post_id ) // Check the user's permissions.
|| ! wp_verify_nonce( $_POST['woocommerce_custom_extra_tab_meta_box_nonce'], 'woocommerce_custom_extra_tab_meta_box_nonce' ) ) {
return $post_id;
}
if ( isset ( $_POST['custom_tab_label'] ) ) {
update_post_meta( $post_id, 'custom_tab_label', $_POST['custom_tab_label'] );
}
else {
delete_post_meta( $post_id, 'custom_tab_label' );
}
if ( isset ( $_POST['custom_tab_content'] ) ) {
update_post_meta( $post_id, 'custom_tab_content', $_POST['custom_tab_content'] );
}
else {
delete_post_meta( $post_id, 'custom_tab_content' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment