Skip to content

Instantly share code, notes, and snippets.

@jamesmthornton
Created June 26, 2016 02:49
Show Gist options
  • Save jamesmthornton/1b0f0ffbeaa38d25cc87139596b1262f to your computer and use it in GitHub Desktop.
Save jamesmthornton/1b0f0ffbeaa38d25cc87139596b1262f to your computer and use it in GitHub Desktop.
Add Custom Text Field to Woocommerce Products Pages Post Type using Woocommerce Functions and Hooks
// Create Woocommerce Custom Field (here called Base Name)
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group custom_wc_field">';
woocommerce_wp_text_input( // Make our text field with the following
array(
'id' => 'base_name',
'label' => __( 'Base Name Field', 'woocommerce' ),
'placeholder' => 'base name text here',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' )
)
);
echo '</div>';
}
// Save to the wpbd when we update the product pages (post)
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_text_field = $_POST['base_name'];
if( !empty( $woocommerce_text_field ) ) update_post_meta( $post_id, 'base_name', esc_attr( $woocommerce_text_field ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment