-
-
Save flabernardez/0f7f50aea9ba9c5a00afdaea54d73a87 to your computer and use it in GitHub Desktop.
#wordpress Añadir avisos en el dashboard
This file contains 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); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment