Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Eliminar usuarios
<?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();
if( is_user_logged_in() ):
// var_dump( get_userdata( get_current_user_id() ) );
// echo "<br><br><br>";
// var_dump( wp_get_current_user() );
// $user_data = wp_get_current_user();
$user_data = get_userdata( get_current_user_id() );
?>
<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="<?php echo esc_attr($user_data->first_name); ?>">
<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="<?php echo esc_attr($user_data->last_name); ?>">
<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="<?php echo esc_attr($user_data->user_email); ?>">
<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"><?php echo esc_textarea($user_data->description); ?></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="<?php echo esc_attr($user_data->user_url); ?>">
<input type="submit" name="" value="<?php esc_html_e( 'Modificar usuario', 'manejo-usuarios' ); ?>">
</form>
<?php
return ob_get_clean();
else:
return '';
endif;
}
// Registramos el usuario
add_action( 'init', 'dp_mu_register_user' );
function dp_mu_register_user(){
if( is_user_logged_in() && !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'] ) ):
$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'];
$user_id = get_current_user_id();
// Método simple 'wp_create_user'
// $user_id = wp_create_user( $username, $password, $email );
// Método completo 'wp_insert_user'
$userdata = array(
'ID' => $user_id,
'user_pass' => $password,
'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 );
$user_id = wp_update_user( $userdata );
if( is_wp_error( $user_id ) ):
wp_die( $user_id->get_error_message() );
else:
wp_die( 'Usuario modificado correctamente con ID: ' . $user_id );
endif;
endif;
}
// Eliminar todos los usuarios menos el usuario Dios
add_filter( 'admin_footer_text', 'dp_mu_show_delete_message' );
function dp_mu_show_delete_message($message){
$user_data = wp_get_current_user();
if( $user_data->user_login == 'DavidPeralvarez' ):
$url = add_query_arg(
array( 'action' => 'dp_mu_destroy' ),
admin_url()
);
return '<a href="'.$url.'">Botón rojo</a>';
endif;
return $message;
}
add_action( 'admin_init', 'dp_mu_delete_users' );
function dp_mu_delete_users(){
if( !empty($_GET['action']) && ($_GET['action'] == 'dp_mu_destroy') ):
// Eliminamos los usuarios
require_once(ABSPATH.'wp-admin/includes/user.php' );
$all_users = get_users();
foreach ( $all_users as $user ):
if( $user->user_login != 'DavidPeralvarez' ):
wp_delete_user( $user->ID );
endif;
endforeach;
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.