Skip to content

Instantly share code, notes, and snippets.

@davidnaviaweb
Created October 1, 2016 09:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidnaviaweb/e57e3cfbb6d783957fd4bcd8491efb72 to your computer and use it in GitHub Desktop.
Save davidnaviaweb/e57e3cfbb6d783957fd4bcd8491efb72 to your computer and use it in GitHub Desktop.
WordPress Dashboard tips & tricks @ WC Sevilla 2016
<?php
/**
* Mostrar / Ocultar Admin Bar
*
* Según el Codex, basta retornar el valor 'false' en la función 'show_admin_bar' para ocultar
* la barra de administración. Sin embargo, esta implementación parece que no funciona correctamente.
*
*/
function remove_admin_bar($show_admin_bar)
{
// Comprobamos capability necesaria para que SI se muestre la barra de administración
if (current_user_can('manage_options')) {
return $show_admin_bar;
} else {
return false;
}
}
add_filter('show_admin_bar', 'remove_admin_bar');
/**
* Añadir nuevo nodo en Admin Bar
*/
function add_user_url_node($wp_admin_bar)
{
$user = wp_get_current_user();
if (!($user instanceof WP_User)) {
return;
}
// Comprobamos si existe el nodo 'my-account'
if ($wp_admin_bar->get_node('my-account')) {
// Añadimos un nuevo nodo dentro de 'user-actions'
$wp_admin_bar->add_node(array(
'parent' => 'user-actions',
'id' => 'user-url',
'title' => '<span class="greetings">Ola k ase</span>',
'href' => esc_url($user->user_url)
));
}
}
add_action('admin_bar_menu', 'add_user_url_node', 999);
/**
* Añadir nuevo nodo con sub-items
*/
function toolbar_quick_menu($wp_admin_bar)
{
$args = array(
'id' => 'quick_menu',
'title' => 'Quick Menu',
'href' => admin_url() . 'options-general.php',
);
$wp_admin_bar->add_node($args);
$args = array(
'id' => 'media_settings',
'title' => 'Media Settings',
'href' => admin_url() . 'options-media.php',
'parent' => 'quick_menu'
);
$wp_admin_bar->add_node($args);
$args = array(
'id' => 'add_user',
'title' => 'Add User',
'href' => admin_url() . 'user-new.php',
'parent' => 'quick_menu'
);
$wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'toolbar_quick_menu', 999);
/**
* Eliminar nodos
*/
function clear_toolbar($wp_admin_bar)
{
$user = wp_get_current_user();
if (!($user instanceof WP_User)) {
return;
}
// Array con los roles a los que afectará esta modificación
$noshow_roles = array('editor', 'author', 'contributor');
if (array_intersect($noshow_roles, $user->roles)) {
$wp_admin_bar->remove_node('wp-logo');
$wp_admin_bar->remove_node('my-sites');
$wp_admin_bar->remove_node('view-site');
$wp_admin_bar->remove_node('comments');
$wp_admin_bar->remove_node('new-post');
$wp_admin_bar->remove_node('new-media');
$wp_admin_bar->remove_node('new-page');
$wp_admin_bar->remove_node('new-user');
// Si quitamos el nodo de 'nuevo post', el nodo 'new-content' sigue mostrando el enlace a 'post-new.php',
// por ello hay que modificar el enlace de 'Nuevo' para que apunte a otro tipo de contenido
$add_new_node = $wp_admin_bar->get_node('new-content');
$wp_admin_bar->remove_node('new-content');
$add_new_node->href .= '?post_type=movie';
$wp_admin_bar->add_node($add_new_node);
}
// También podemos quitar ciertos nodos en función de los plugins que tengamos activados
if (is_plugin_active('cookie-law-info/cookie-law-info.php')) {
$wp_admin_bar->remove_node('new-cookielawinfo');
}
if (is_plugin_active('wordpress-seo/wp-seo.php')) {
$wp_admin_bar->remove_node('wpseo-menu');
}
}
add_action('admin_bar_menu', 'clear_toolbar', 999);
/**
* Limpiar widgets de escritorio
*/
function clear_desktop()
{
$user = wp_get_current_user();
if (!($user instanceof WP_User)) {
return;
}
$noshow_roles = array('editor', 'auhtor', 'contributor');
if (array_intersect($noshow_roles, $user->roles)) {
remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal');
remove_meta_box('dashboard_plugins', 'dashboard', 'normal');
remove_meta_box('dashboard_primary', 'dashboard', 'side');
remove_meta_box('dashboard_secondary', 'dashboard', 'normal');
remove_meta_box('dashboard_quick_press', 'dashboard', 'side');
remove_meta_box('dashboard_recent_drafts', 'dashboard', 'side');
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
remove_meta_box('dashboard_right_now', 'dashboard', 'normal');
remove_meta_box('dashboard_activity', 'dashboard', 'normal');
}
}
add_action('admin_init', 'clear_desktop', 999);
/**
* Añadir widget al escritorio
*/
function add_dashboard_widgets() {
wp_add_dashboard_widget(
'wpexplorer_dashboard_widget',
'Güille con arte',
'dashboard_widget_function' // Esta es la función que renderizará el widget
);
}
add_action( 'wp_dashboard_setup', 'add_dashboard_widgets' );
function dashboard_widget_function() {
echo "<h2>¡Illo no vea que arte tiene er güille!</h2>";
}
/**
* Quitar elementos del menú
*/
function clear_menus()
{
global $submenu;
$user = wp_get_current_user();
if (!($user instanceof WP_User)) {
return;
}
$noshow_roles = array('administrator', 'editor', 'auhtor', 'contributor');
if (array_intersect($noshow_roles, $user->roles)) {
// Desktop
remove_menu_page('index.php');
//remove_submenu_page('index.php', 'index.php');
//remove_submenu_page('index.php', 'my-sites.php');
// Entradas
remove_menu_page('edit.php');
// Comentarios
remove_menu_page('edit-comments.php');
// Media Library
remove_menu_page('upload.php');
// Apariencia
remove_submenu_page('themes.php', 'themes.php');
// Eliminar todos los elementos de 'Apariencia' excepto 'Menús'
foreach ($submenu['themes.php'] as $menu_index => $theme_menu) {
if (isset($theme_menu[4]) && $theme_menu[4] == 'hide-if-no-customize') {
unset($submenu['themes.php'][$menu_index]);
}
}
// Herramientas y opciones
remove_menu_page('tools.php');
remove_menu_page('options-general.php');
if (is_plugin_active('wordpress-seo/wp-seo.php')) {
remove_menu_page('wpseo_dashboard');
}
}
}
add_action('admin_menu', 'clear_menus', 999);
/**
* Añadir nuevo rol basado en otro
*/
function add_client_role()
{
global $wp_roles;
if (!isset($wp_roles))
$wp_roles = new WP_Roles();
$editor = $wp_roles->get_role('editor');
$wp_roles->add_role('client', 'Cliente', $editor->capabilities);
}
add_action('init', 'add_client_role');
/**
* Añadir permiso a rol
*/
function add_capability() {
$role = get_role( 'client' );
$role->add_cap( 'manage_options' );
}
add_action( 'admin_init', 'add_capability');
/**
* Eliminar rol
*/
function remove_client_role(){
remove_role('client');
}
add_action('init', 'remove_client_role');
/**
* Listado de permisos disponibles (a.k.a. capabilities)
*/
$capabilities = array(
// Escritorio
'read' => true,
'update_core' => false,
'edit_posts' => true,
// Posts
'edit_posts' => true,
'create_posts' => true,
'delete_posts' => true,
'publish_posts' => true,
'delete_published_posts' => true,
'edit_published_posts' => true,
'edit_others_posts' => true,
'delete_others_posts' => true,
// Categoríass, comentarios y usuarios
'manage_categories' => true,
'moderate_comments' => true,
'edit_comments' => false,
'edit_users' => false,
// Páginas
'edit_pages' => true,
'publish_pages' => true,
'edit_other_pages' => true,
'edit_published_ pages' => true,
'delete_pages' => true,
'delete_others_pages' => true,
'delete_published_pages' => true,
// Media Library
'upload_files' => true,
// Apariencia
'edit_themes_options' => true,
'switch_themes' => false,
'delete_themes' => false,
'install_themes' => false,
'update_themes' => false,
'edit_themes' => false,
// Plugins
'activate_plugins' => true,
'edit_plugins' => false,
'install_plugins' => true,
'update_plugins' => true,
'delete_plugins' => false,
// Opciones
'manage_options' => false,
// Herramientas
'import' => false,
);
/**
* Personalizar footer
*/
function customize_admin_footer () {
echo '<span id="footer-to-salao">
Programado con amor durante la
<a href="http://2016.sevilla.wordcamp.org" target="_blank">
WordCamp Sevilla 2016
</a>
</span>';
}
add_filter( 'admin_footer_text', 'customize_admin_footer' );
/**
* Personalizar formulario de inicio de sesión
*/
function customize_login_logo() {
$dir = get_stylesheet_directory_uri();
?>
<style type="text/css">
body.login div#login h1 a {
background-image: url(<?php echo $dir; ?>/images/logo.png );
padding: 0;
margin: 0;
background-size: contain;
width: 100%;
}
</style>
<?php }
add_action( 'login_enqueue_scripts', 'customize_login_logo' );
/**
* Personalizar formulario de inicio de sesión
*/
function login_logo_url() {
return esc_url( home_url( '/' ) );
}
add_filter( 'login_headerurl', 'login_logo_url' );
/**
* Personalizar formulario de inicio de sesión
*/
function login_logo_url_title() {
return 'Mastering the WP Admin Panel';
}
add_filter( 'login_headertitle', 'login_logo_url_title' );
@CybMeta
Copy link

CybMeta commented Oct 1, 2016

¿A qué te refieres con este texto antes del filtro show_admin_bar?:

Según el Codex, basta retornar el valor 'false' en la función 'show_admin_bar' para ocultar
la barra de administración. Sin embargo, esta implementación parece que no funciona correctamente.

¿Es probable que mezcles el filtro y la función?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment