Last active
April 24, 2022 06:33
-
-
Save widoz/f0fa51370868adf11e81113daaedeaca to your computer and use it in GitHub Desktop.
Helper function to show a WordPress 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 | |
function notify(string $message, string $noticeType, array $allowedMarkup = []): void | |
{ | |
$callback = function () use ($message, $noticeType, $allowedMarkup) { | |
?> | |
<div class="notice notice-<?= \sanitize_html_class($noticeType) ?>"> | |
<p><?= \wp_kses($message, $allowedMarkup) ?></p> | |
</div> | |
<?php | |
}; | |
if (\did_action('admin_notices') && \defined('WP_DEBUG') && WP_DEBUG) { | |
throw new \LogicException('Cannot call `admin_notices`, the action has been executed.') | |
} | |
if (\doing_action('admin_notices')) { | |
$callback(); | |
return; | |
} | |
\add_action('admin_notices', $callback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment