У меня есть этот код для добавления настраиваемого поля в профиль пользователя. Он работает нормально, но полезен только для типа ввода «текст».
Мне нужно добавить тип ввода «радио». Мне нужна помощь с кодом.
/***********************************************************/
/********** Custom User field ************/
/***********************************************************/
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) {
?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class = "form-table">
<tr>
<th><label for = "equivalence_surcharge"><?php _e("Equivalence Surcharge"); ?></label></th>
<td>
<input type = "text" name = "equivalence_surcharge" id = "equivalence_surcharge" value = "<?php echo esc_attr( get_the_author_meta( 'equivalence_surcharge', $user->ID ) ); ?>" class = "regular-text" /><br />
<span class = "description"><?php _e("Equivalence Surcharge"); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'update-user_' . $user_id ) ) {
return;
}
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'equivalence_surcharge', $_POST['equivalence_surcharge'] );
}






Чтобы добавить поле ввода переключателей в профиль пользователя, используйте следующее:
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) {
$meta_key = 'equivalence_surcharge'; // <== Define the meta key
$meta_value = get_user_meta( $user->id, $meta_key, true ); // Get meta value
$index = 0; // Initialize
// Define radio button options option value / label pairs, in the array
$options = array(
'option_1' => __('Option 1', 'text-domain'),
'option_2' => __('Option 2', 'text-domain'),
'option_3' => __('Option 3', 'text-domain'),
);
echo '<style>.extra-infos label{display:block; padding:2px 0}</style>'; // CSS inline styles
echo '<h3>'. __('Extra profile information', 'text-domain') . '</h3>
<table class = "extra-infos"><tr>
<th style = "text-align:left; padding:0 0 8px">'.__('Equivalence surcharge', 'text-domain').'</th></tr>
<tr><td>';
foreach ( $options as $option => $label ) {
printf('<label for = "radio_%d">
<input type = "radio" id = "input-radio_%d" class = "input-radio" name = "%s" value = "%s" %s> %s
</label>',
$index, $index, $meta_key, $option, checked($option, $meta_value, false), $label );
$index++;
}
echo '</td></tr></table><br>';
}
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'update-user_' . $user_id ) ) {
return;
}
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return;
}
$meta_key = 'equivalence_surcharge'; // Define the meta key
$meta_value = isset($_POST[$meta_key]) ? esc_attr($_POST[$meta_key]) : '';
update_user_meta( $user_id, $meta_key, $meta_value );
}
Код находится в файле function.php вашей дочерней темы (или в плагине). Протестировано и работает.
Вы получите что-то вроде:
Отображение переключателей подряд (горизонтально):
Вы можете отобразить переключатели подряд, изменив:
echo '<style>.extra-infos label{display:block; padding:2px 0}</style>'; // CSS inline styles
с:
echo '<style>.extra-infos label{display:inline-block; padding:2px 20px 2px 0}</style>'; // CSS inline styles
и вы получите что-то вроде:
И еще один вопрос. Могу ли я показать это поле в регистрационной форме?
Можете ли вы добавить метку? Захват: drive.google.com/file/d/1PXGEp6oKmW5GmnN2oehwKi6KhP1wVuar/…