Created
June 26, 2016 02:49
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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