Last active
October 30, 2022 15:03
-
-
Save glueckpress/6befeb937da89025d4d8 to your computer and use it in GitHub Desktop.
[WordPress] Sample Admin Notices
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 | |
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' ); |
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
<div class="notice notice-success"> | |
<p>All is well.</p> | |
</div> |
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 | |
/** | |
* 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' ); |
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
<div class="notice notice-success is-dismissible"> | |
<p>All is well.</p> | |
</div> |
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 | |
/** | |
* 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 ); | |
} | |
} |
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 | |
/** | |
* 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' ); |
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 | |
/** | |
* 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' ); |
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 | |
/** | |
* 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' ); |
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)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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