Skip to content

Instantly share code, notes, and snippets.

@flabernardez
Forked from PCianes/admin_notice.php
Last active April 17, 2018 11:35
Show Gist options
  • Save flabernardez/0f7f50aea9ba9c5a00afdaea54d73a87 to your computer and use it in GitHub Desktop.
Save flabernardez/0f7f50aea9ba9c5a00afdaea54d73a87 to your computer and use it in GitHub Desktop.
#wordpress Añadir avisos en el dashboard
<?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