En el siguiente ejemplo vamos a crear un plugin para crear usuarios con permisos personalizados WordPress. El plugin al activarlo creará el usuario 'pepe' con una serie de permisos que definiremos antes. El usuario tendrá acceso a todo el área de administración de WordPress pero no podrá modificar nada.
Plugin para WordPress Usuario con permisos personalizados:
<?php /* Plugin Name: Gbi UsuarioCustom Author: Jesús García Author URI: https://garber.es Description: Crea el usuario 'pepe' con los permisos adecuados Author URI: https://garber.es License: GPLv2 or later Text Domain: gbi */ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Copyright 2005-2018 garber.es */ // esta función define el rol 'pepe' y agrega los permisos adecuados function gbi_rol_pepe(){ remove_role('pepe'); // si existe lo eliminamos add_role('pepe', 'Usuario Pepes', array( 'activate_plugins' => false, 'create_posts' => false, 'create_users' => false, 'delete_others_pages' => false, 'delete_others_posts' => false, 'delete_pages' => false, 'delete_plugins' => false, 'delete_posts' => false, 'delete_private_pages' => false, 'delete_private_posts' => false, 'delete_published_pages' => false, 'delete_published_posts' => false, 'delete_themes' => false, 'delete_users' => false, 'edit_dashboard' => false, 'edit_others_pages' => false, 'edit_others_posts' => false, 'edit_pages' => true, 'edit_plugins' => false, 'edit_posts' => true, 'edit_private_pages' => false, 'edit_private_posts' => false, 'edit_published_pages' => false, 'edit_published_posts' => false, 'edit_theme_options' => false, 'edit_themes' => false, 'edit_users' => false, 'export' => false, 'import' => false, 'install_plugins' => false, 'install_themes' => false, 'list_users' => true, 'manage_categories' => false, 'manage_links' => false, 'manage_options' => false, 'moderate_comments' => false, 'promote_users' => false, 'publish_pages' => false, 'publish_posts' => false, 'read' => true, 'read_private_pages' => false, 'read_private_posts' => false, 'remove_users' => false, 'switch_themes' => false, 'unfiltered_html' => false, 'unfiltered_upload' => false, 'update_core' => false, 'update_plugins' => false, 'update_themes' => false, 'upload_files' => false, 'wpseo_bulk_edit' => false, 'wpseo_edit_advanced_metadata' => false, 'wpseo_manage_options' => false, )); $userdata = array( 'user_login' => 'pepe', 'user_pass' => 'ContraseñaPepe', 'user_email' => 'pepe@google.com', 'role' => 'pepe' ); $user_id = wp_insert_user( $userdata ) ; } register_activation_hook( __FILE__ , 'gbi_rol_pepe' ); // lo anclamos al activar el plugin ?>
Las funciones utilizadas son add_role() para crear el rol y asignar los permisos que queremos. Los parámetros son el nombre del rol, el nombre a mostrar y un array con los distintos permisos.
<?php add_role( $role, $display_name, $capabilities ); ?>Otra función interesante es wp_insert_user( $userdata ), que inserta/actualiza el usuario pasado en el array $userdata.
<?php wp_insert_user( $userdata ); ?>Por último anclamos la función que crea el rol y añade el usuario al activar el plugin con el hook register_activation_hook.
<?php register_activation_hook( $file, $function ); ?>Espero que os sirva.