text 用于在woocommerce管理区域中添加和保存自定义字段的代码段
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 用于在woocommerce管理区域中添加和保存自定义字段的代码段相关的知识,希望对你有一定的参考价值。
// 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 );
}
以上是关于text 用于在woocommerce管理区域中添加和保存自定义字段的代码段的主要内容,如果未能解决你的问题,请参考以下文章
Wordpress登录页面重定向到普通用户的Woocommerce我的帐户页面