Last active
January 11, 2019 08:22
-
-
Save PCianes/bd3148ee2c01fe4a542f3e46b56c139b to your computer and use it in GitHub Desktop.
Admin notices (avisos panel administración)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Some example snippets for the control of notices in WordPress admin panel | |
* | |
* @package PCianes\AdminUtilities | |
* @since 1.0.0 | |
* @author PCianes | |
* @license GNU General Public License 2.0+ | |
*/ | |
namespace PCianes\AdminUtilities; | |
/** | |
* Add condicional notice in admin (example) | |
* | |
* @since 1.0.0 | |
* | |
* @return void | |
*/ | |
function add_admin_notice(){ | |
$admin_page_with_notice = 'index.php'; | |
$user_role_for_notice = 'administrator'; | |
global $pagenow; | |
if ( !($pagenow == $admin_page_with_notice) ) { | |
return; | |
} | |
$current_user = wp_get_current_user(); | |
if ( !in_array( $user_role_for_notice , (array) $current_user->roles ) ) { | |
return; | |
} | |
printf('<div class="notice notice-info is-dismissible"> | |
<p>Click on <a href="edit.php">Posts</a> to start writing.</p> | |
<h1>HELLO WORLD!! This page is %s and you are %s</h1> | |
</div>', | |
$admin_page_with_notice, $user_role_for_notice); | |
} | |
add_action('admin_notices', __NAMESPACE__ . '\add_admin_notice', 11); | |
/** | |
* Delete all admin notices except for one user_ID | |
* | |
* @since 1.0.0 | |
* | |
* @return void | |
*/ | |
function delete_all_admin_notices() { | |
$admin_user_ID_with_notices = '1'; | |
$user_role_for_notices = 'administrator'; | |
$current_user = wp_get_current_user(); | |
$current_user_data = $current_user->data; | |
if ( in_array( $user_role_for_notices, (array) $current_user->roles ) | |
&& $current_user_data->ID == $admin_user_ID_with_notices ) { | |
return; | |
} | |
remove_all_filters( 'admin_notices'); | |
} | |
add_action('admin_init', __NAMESPACE__ . '\delete_all_admin_notices', 999); | |
Gracias David por tu aporte, ciertamente no he usado get_current_user_id para tener mas datos para variaciones con wp_get_current_user , pero ayer al hacer la prueba rápida si que había metido mano a una variable global como la que controla los eventos mediante $wp_filter que siempre es peligroso. ;-)
Genial David! Ya lo he cambiado y me convence mucho más así. ¡gracias por tu ayuda!
He editado el código de arriba con las modificaciones. :) Gracias!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muchas gracias por los comentarios! Acabo de reescribir el código documentado todo como recomendabais pero intentando que el orden y estructura del código, así como que los nombres de las variables y funciones sirvan para entenderlo sin necesidad de más comentarios añadidos.