[英]Adding some my account custom fields to admin user pages in Woocommerce
更新- 要在 Wordpress 管理用戶頁面中添加這兩個“最喜歡的顏色”自定義字段,您將使用以下內容:
// Add the custom field "favorite_color" in admin user
add_action( 'show_user_profile', 'add_extra_custom_user_data', 1, 1 );
add_action( 'edit_user_profile', 'add_extra_custom_user_data', 1, 1 );
function add_extra_custom_user_data( $user )
{
?>
<h3><?php _e("Other details",'woocommerce' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="favorite_color"><?php _e( 'Favorite color', 'woocommerce' ); ?></label></th>
<td><input type="text" name="favorite_color" value="<?php echo esc_attr(get_the_author_meta( 'favorite_color', $user->ID )); ?>" class="regular-text" /></td>
</tr><tr>
<th><label for="favorite_color2"><?php _e( 'Favorite color 2', 'woocommerce' ); ?></label></th>
<td><input type="text" name="favorite_color2" value="<?php echo esc_attr(get_the_author_meta( 'favorite_color2', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<br />
<?php
}
// Save the custom field 'favorite_color' in admin user
add_action( 'personal_options_update', 'save_extra_custom_user_data' );
add_action( 'edit_user_profile_update', 'save_extra_custom_user_data' );
function save_extra_custom_user_data( $user_id )
{
if( ! empty($_POST['favorite_color']) )
update_user_meta( $user_id, 'favorite_color', sanitize_text_field( $_POST['favorite_color'] ) );
if( ! empty($_POST['favorite_color2']) )
update_user_meta( $user_id, 'favorite_color2', sanitize_text_field( $_POST['favorite_color2'] ) );
}
代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.