Skip to content

Instantly share code, notes, and snippets.

@DavidPeralvarez
Created July 25, 2019 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidPeralvarez/273d8167ca541d246f9d9ed61d713279 to your computer and use it in GitHub Desktop.
Save DavidPeralvarez/273d8167ca541d246f9d9ed61d713279 to your computer and use it in GitHub Desktop.
Añadir sección en el perfil y página de edición de usuarios
<?php
/*
Plugin Name: Ajustes Usuario
Description: Aprendiendo a usar los ajustes de usuario
Text Domain: ajustes-usuario
*/
// Añadir campos en el perfil del usuario
add_action( 'show_user_profile', 'dp_au_add_fields' );
add_action( 'edit_user_profile', 'dp_au_add_fields' );
function dp_au_add_fields( $user ){
$direccion = get_user_meta( $user->ID, 'dp_au_direccion', true );
$nif = get_user_meta( $user->ID, 'dp_au_nif', true );
?>
<h3><?php esc_html_e( 'Datos fiscales', 'ajustes-usuario' ); ?></h3>
<table class="form-table">
<tr>
<th>
<label for="direccion"><?php esc_html_e( 'Dirección', 'ajustes-usuario' ); ?></label>
</th>
<td>
<input type="text" class="regular-text" name="direccion" id="direccion" value="<?php echo esc_attr( $direccion ); ?>">
<p class="description"><?php esc_html_e( 'Introduce tu dirección completa.', 'ajustes-usuario' ); ?></p>
</td>
</tr>
<tr>
<th>
<label for="nif"><?php esc_html_e( 'NIF', 'ajustes-usuario' ); ?></label>
</th>
<td>
<input type="text" class="regular-text" name="nif" id="nif" value="<?php echo esc_attr( $nif ); ?>">
<p class="description"><?php esc_html_e( 'Introduce tu número de identificación fiscal.', 'ajustes-usuario' ); ?></p>
</td>
</tr>
</table>
<?php
}
// Guardar los metadatos del usuario
add_action( 'personal_options_update', 'dp_au_save_fields' );
add_action( 'edit_user_profile_update', 'dp_au_save_fields' );
function dp_au_save_fields( $user_id ){
update_user_meta( $user_id, 'dp_au_direccion', $_POST['direccion'] );
update_user_meta( $user_id, 'dp_au_nif', $_POST['nif'] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment