Skip to content

Instantly share code, notes, and snippets.

@widoz
Last active April 24, 2022 06:33
Show Gist options
  • Save widoz/f0fa51370868adf11e81113daaedeaca to your computer and use it in GitHub Desktop.
Save widoz/f0fa51370868adf11e81113daaedeaca to your computer and use it in GitHub Desktop.
Helper function to show a WordPress admin notice
<?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