Getting notice functionality in the admin panel

This commit is contained in:
2021-10-06 12:09:37 -06:00
parent af75b9544a
commit fa1807f5fc
5 changed files with 61 additions and 9 deletions

View File

@ -0,0 +1,48 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Show a notice on the admin panel.
*
* @since 1.0.0
*
* @param string $message The message to show in the notice
* @param string $type The 'type' of notice, e.g. info (default), success, warning, or error
* @param bool $dismissible Whether the notice should be dismissible
*
* @return string
*/
function nmsp_admin_notice( string $message, string $type = 'info', bool $dismissible = true ) {
$classes = 'notice';
switch ($type) {
case 'success':
$classes .= ' notice-success';
break;
case 'warning':
$classes .= ' notice-warning';
break;
case 'error':
$classes .= ' notice-error';
break;
case 'info':
default:
$classes .= ' notice-info';
}
if ( $dismissible ) {
$classes .= ' is-dismissible';
}
add_action( 'admin_notices', function () use ( $classes, $message ) {
ob_start();
?>
<div class="<?php echo $classes; ?>">
<p><?php _e( $message, 'nmsp-plugin-name-text-domain' ); ?></p>
</div>
<?php
echo ob_get_clean();
});
}