Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Sample Admin Notices
<?php
defined( 'ABSPATH' ) or die( 'Poop.' );
/**
* Plugin Name: _Sample Admin Notice
* Description: Displays a sample admin notice once for admins. (Requires PHP 5.3+.)
* Version: 0.1
* Author: Caspar Hübinger
* Plugin URI: https://gist.github.com/glueckpress/6befeb937da89025d4d8#file-sample-admin-notice-php
* Author URI: https://profiles.wordpress.org/glueckpress
* License: GNU General Public License v3
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
if ( version_compare( phpversion(), '5.3' ) < 0 )
return;
/**
* Render sample admin notice.
* @param string $message Message text (HTML)
* @param string $classes CSS classes, separate by spaces
* @return void
*/
function sample_admin_notice__render_notice( $message = '', $classes = 'notice-success' ) {
if ( ! empty( $message ) ) {
printf( '<div class="notice %2$s">%1$s</div>', $message, $classes );
}
}
/**
* Display admin notice once, then never again.
* @return void
*/
function sample_admin_notice() {
$maybe_display_notice = get_option( 'sample_admin_notice__status', 'activating' );
if ( 'activating' === $maybe_display_notice && current_user_can( 'manage_options' ) ) {
add_action( 'admin_notices', function() {
$user = $GLOBALS['current_user'];
$message = sprintf( '<p><strong>Sample Admin Notice</strong> funzt, %s!</p>', $user->display_name );
sample_admin_notice__render_notice( $message, 'notice-info is-dismissible' );
} );
update_option( 'sample_admin_notice__status', 'activated' );
}
}
add_action( 'init', 'sample_admin_notice' );
<div class="notice notice-success">
<p>All is well.</p>
</div>
<?php
/**
* Simple Admin Notice
* @return void
*/
function sample_admin_notice__success() { ?>
<div class="notice notice-success">
<p><strong>Works!</strong></p>
</div>
<?php
}
add_action( 'admin_notices', 'sample_admin_notice__success' );
<div class="notice notice-success is-dismissible">
<p>All is well.</p>
</div>
<?php
/**
* Render sample admin notice.
* @param string $message Message text (HTML)
* @param string $classes CSS classes, separate by spaces
* @return void
*/
function sample_admin_notice__render_notice( $message = '', $classes = 'notice-success' ) {
if ( ! empty( $message ) ) {
printf( '<div class="notice %2$s">%1$s</div>', $message, $classes );
}
}
<?php
/**
* Display admin notice once, then never again.
* @uses sample_admin_notice__render_notice()
* @return void
*/
function sample_admin_notice() {
$maybe_display_notice = get_option( 'sample_admin_notice__status', 'activating' );
if ( 'activating' === $maybe_display_notice && current_user_can( 'manage_options' ) ) {
add_action( 'admin_notices', function() {
$user = $GLOBALS['current_user'];
$message = sprintf( '<p><strong>Sample Admin Notice</strong> funzt, %s!</p>', $user->display_name );
sample_admin_notice__render_notice( $message, 'notice-info is-dismissible' );
} );
update_option( 'sample_admin_notice__status', 'activated' );
}
}
add_action( 'init', 'sample_admin_notice' );
<?php
/**
* DON’T USE!
* No good for use in plugins/themes: .update-nag.
* @return void
*/
function sample_admin_notice__update_nag() { ?>
<div class="update-nag">
<p><code>.update-nag</code> would trigger this notice to be displayed above site titles in WP Admin.</p>
</div>
<?php
}
add_action( 'admin_notices', 'sample_admin_notice__update_nag' );
<?php
/**
* DON’T USE!
* Worse than .update-nag, as it fails to clear.
* @return void
*/
function sample_admin_notice__update_nag_notice() { ?>
<div class="update-nag notice">
<p>Doesn’t clear well: <code>.update-nag.notice</code> will turn WP Admin into a mess.</p>
</div>
<?php
}
add_action( 'admin_notices', 'sample_admin_notice__update_nag_notice' );
@ZeeshanAslam123
Copy link

ZeeshanAslam123 commented Sep 28, 2020

i'm using admin_notices action hook after WordPress custom menu page form submission with object oriented method so what can i do for that. but still not response action hook didn't run with inside of public function so what can i do

Loading

@divyrajch
Copy link

divyrajch commented Oct 28, 2021

How to display admin_notice from my plugin PHP file
because I write this code in my other function but this action not called
add_action( 'admin_notices', 'call_notice'); function call_notice(){ echo '<div class="notice notice-error is-dismissible"> <p><b style="font-size:15px;">You are Called.</b></p> </div>'; }
I put add action in my else part (when if condition became wrong)

Loading

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