Появилась задача отобразить на странице пользователя в админке Wordpress некоторую информацию. Есть ли какой-то крючок, что бы добавить сюда что-нибудь? А так же можно ли как-то узнать id пользователя, страница которого открыта?
1 ответ
Вот рабочий код. Исправьте поля под ваши нужды и добавьте код в functions.php
вашей темы.
/**
* Add extra fields to user profile.
*
* @param WP_User $user WP_User object.
*/
function extra_user_profile_fields( $user ) {
?>
<h3><?php esc_html_e( 'Дополнительная информация профиля', 'my-lang-domain' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="phone"><?php esc_html_e( 'Телефон', 'my-lang-domain' ); ?></label></th>
<td>
<input
type="text" name="phone" id="phone"
value="<?php echo esc_attr( get_user_meta( $user->ID, 'phone', true ) ); ?>"
class="regular-text"/><br/>
</td>
</tr>
<tr>
<th><label for="manager"><?php _e( 'Личный менеджер', 'my-lang-domain' ); ?></label></th>
<td>
<select name="manager" id="manager" style="width: 25em;">
<?php
$selected = esc_attr( get_user_meta( $user->ID, 'manager', true ) );
$users = get_users();
$options = array();
$options[0] = 'Выберите менеджера…';
foreach ( $users as $a_user ) {
$options[ $a_user->ID ] = $a_user->display_name;
}
foreach ( $options as $option_key => $option_value ) :
?>
<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $selected, $option_key, true ); ?>><?php echo esc_attr( $option_value ); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
/**
* Save extra fields in user profile.
*
* @param int $user_id User ID.
*
* @return bool
*/
function save_extra_user_profile_fields( $user_id ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
$result = true;
if ( isset( $_POST['phone'] ) ) {
$result = $result && update_user_meta( $user_id, 'phone', wc_clean( $_POST['phone'] ) );
}
if ( isset( $_POST['manager'] ) ) {
$result = $result && update_user_meta( $user_id, 'manager', wc_clean( $_POST['manager'] ) );
}
return $result;
}
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );