Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Crear usuarios con método completo
<?php
/*
Plugin Name: Manejo de Usuarios
Description: Aprendiendo a gestionar usuarios en WordPress
Text Domain: manejo-usuarios
*/
// Formulario de registro
add_shortcode( 'dp_mu_register_form', 'dp_mu_register_form_func' );
function dp_mu_register_form_func(){
ob_start();
?>
<form id="dp_mu_register_form" class="dp_mu_register_form" action="" method="post">
<label for="dp_mu_username"><?php esc_html_e( 'Nombre de usuario', 'manejo-usuarios' ); ?></label>
<input type="text" id="dp_mu_username" name="dp_mu_username" value="">
<label for="dp_mu_firstname"><?php esc_html_e( 'Nombre', 'manejo-usuarios' ); ?></label>
<input type="text" id="dp_mu_firstname" name="dp_mu_firstname" value="">
<label for="dp_mu_lastname"><?php esc_html_e( 'Apellidos', 'manejo-usuarios' ); ?></label>
<input type="text" id="dp_mu_lastname" name="dp_mu_lastname" value="">
<label for="dp_mu_email"><?php esc_html_e( 'Correo', 'manejo-usuarios' ); ?></label>
<input type="text" id="dp_mu_email" name="dp_mu_email" value="">
<label for="dp_mu_password"><?php esc_html_e( 'Contraseña', 'manejo-usuarios' ); ?></label>
<input type="text" id="dp_mu_password" name="dp_mu_password" value="">
<label for="dp_mu_bio"><?php esc_html_e( 'Mini bio', 'manejo-usuarios' ); ?></label>
<textarea name="dp_mu_bio" id="dp_mu_bio"></textarea>
<label for="dp_mu_website"><?php esc_html_e( 'Sitio web', 'manejo-usuarios' ); ?></label>
<input type="text" id="dp_mu_website" name="dp_mu_website" value="">
<input type="submit" name="" value="<?php esc_html_e( 'Registrar usuario', 'manejo-usuarios' ); ?>">
</form>
<?php
return ob_get_clean();
}
// Registramos el usuario
add_action( 'init', 'dp_mu_register_user' );
function dp_mu_register_user(){
if( !empty( $_POST['dp_mu_username'] ) && !empty( $_POST['dp_mu_email'] ) && !empty( $_POST['dp_mu_password'] ) && !empty( $_POST['dp_mu_firstname'] ) && !empty( $_POST['dp_mu_firstname'] ) && !empty( $_POST['dp_mu_lastname'] ) && !empty( $_POST['dp_mu_website'] ) && !empty( $_POST['dp_mu_bio'] ) ):
$username = $_POST['dp_mu_username'];
$firstname = $_POST['dp_mu_firstname'];
$lastname = $_POST['dp_mu_lastname'];
$bio = $_POST['dp_mu_bio'];
$website = $_POST['dp_mu_website'];
$email = $_POST['dp_mu_email'];
$password = $_POST['dp_mu_password'];
// Método simple 'wp_create_user'
// $user_id = wp_create_user( $username, $password, $email );
// Método completo 'wp_insert_user'
$userdata = array(
'user_pass' => $password,
'user_login' => $username,
'user_url' => $website,
'user_email' => $email,
'first_name' => $firstname,
'last_name' => $lastname,
'display_name' => $firstname. ' ' .$lastname,
'description' => $bio,
'show_admin_bar_front' => 'false',
// 'role' => ''
);
$user_id = wp_insert_user( $userdata );
if( is_wp_error( $user_id ) ):
wp_die( $user_id->get_error_message() );
else:
wp_die( 'Usuario añadido correctamente con ID: ' . $user_id );
endif;
endif;
}
// Añadir estilos
add_action( 'wp_enqueue_scripts', 'dp_mu_enqueue_styles' );
function dp_mu_enqueue_styles(){
wp_register_style( 'dp-mu-styles', plugins_url( 'manejo-usuarios/css/style.css' ) );
wp_enqueue_style( 'dp-mu-styles' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.