Created
March 9, 2018 06:48
-
-
Save pravinnath/24fdd38425b56426e694c52d45e1b119 to your computer and use it in GitHub Desktop.
Snippet to add and save custom fields in woocommerce admin area
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
// Display Fields | |
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' ); | |
// Save Fields | |
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' ); | |
function woo_add_custom_general_fields() { | |
global $woocommerce, $post; | |
echo '<div class="options_group">'; | |
// Chapters Field | |
woocommerce_wp_text_input( | |
array( | |
'id' => '_chapters_field', | |
'label' => __( 'Chapters', 'woocommerce' ), | |
'placeholder' => '', | |
'description' => __( 'Enter the number of chapters here.', 'woocommerce' ), | |
'type' => 'number', | |
'custom_attributes' => array( | |
'step' => 'any', | |
'min' => '0' | |
) | |
) | |
); | |
// Author Field | |
woocommerce_wp_text_input( | |
array( | |
'id' => '_author_field', | |
'label' => __( 'Author', 'woocommerce' ), | |
'placeholder' => 'Book Author Name Here', | |
'desc_tip' => 'true', | |
) | |
); | |
// Editor Field | |
woocommerce_wp_text_input( | |
array( | |
'id' => '_editor_field', | |
'label' => __( 'Editor', 'woocommerce' ), | |
'placeholder' => 'Book Editor Name Here', | |
'desc_tip' => 'true', | |
) | |
); | |
// Translator Field | |
woocommerce_wp_text_input( | |
array( | |
'id' => '_translator_field', | |
'label' => __( 'Translator', 'woocommerce' ), | |
'placeholder' => 'Book Translator Name Here', | |
'desc_tip' => 'true', | |
) | |
); | |
echo '</div>'; | |
} | |
//Code to save all the custom meta fields | |
function woo_add_custom_general_fields_save( $post_id ){ | |
// Author Field | |
$woocommerce_text_field = $_POST['_author_field']; | |
if( !empty( $woocommerce_text_field ) ) | |
update_post_meta( $post_id, '_author_field', esc_attr( $woocommerce_text_field ) ); | |
// Editor Field | |
$woocommerce_text_field = $_POST['_editor_field']; | |
if( !empty( $woocommerce_text_field ) ) | |
update_post_meta( $post_id, '_editor_field', esc_attr( $woocommerce_text_field ) ); | |
// Translator Field | |
$woocommerce_text_field = $_POST['_translator_field']; | |
if( !empty( $woocommerce_text_field ) ) | |
update_post_meta( $post_id, '_translator_field', esc_attr( $woocommerce_text_field ) ); | |
// Chapters Field | |
$woocommerce_number_field = $_POST['_chapters_field']; | |
if( !empty( $woocommerce_number_field ) ) | |
update_post_meta( $post_id, '_chapters_field', esc_attr( $woocommerce_number_field ) ); | |
// Textarea | |
$woocommerce_textarea = $_POST['_textarea']; | |
if( !empty( $woocommerce_textarea ) ) | |
update_post_meta( $post_id, '_textarea', esc_html( $woocommerce_textarea ) ); | |
// Select | |
$woocommerce_select = $_POST['_select']; | |
if( !empty( $woocommerce_select ) ) | |
update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) ); | |
// Checkbox | |
$woocommerce_checkbox = isset( $_POST['_checkbox'] ) ? 'yes' : 'no'; | |
update_post_meta( $post_id, '_checkbox', $woocommerce_checkbox ); | |
// Custom Field | |
$custom_field_type = array( esc_attr( $_POST['_field_one'] ), esc_attr( $_POST['_field_two'] ) ); | |
update_post_meta( $post_id, '_custom_field_type', $custom_field_type ); | |
// Hidden Field | |
$woocommerce_hidden_field = $_POST['_hidden_field']; | |
if( !empty( $woocommerce_hidden_field ) ) | |
update_post_meta( $post_id, '_hidden_field', esc_attr( $woocommerce_hidden_field ) ); | |
// Product Field Type | |
$product_field_type = $_POST['product_field_type']; | |
update_post_meta( $post_id, '_product_field_type_ids', $product_field_type ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment