Solved, but there are probably better ways?
/**
* Notice utility function. Adds WC_notice if not admin page, adds admin notice if admin page
*/
function dd_notice($message, $type, $userid)
{
if (is_admin()) {
update_option('dd_notice', $message);
update_option('dd_notice_userid', $userid);
} else {
wc_add_notice(__($message, 'woocommerce'), $type);
}
}
function dd_add_notice()
{
if (get_option('dd_notice')) {
//Get the URL of the current page
$currentUrl = wp_get_referer();
//Check if URL contains query parameters and set ? or & as character
if (!strpos($currentUrl, '?')) {
$queryChar = '?';
} else {
$queryChar = '&';
}
//Notice to be added
echo sprintf(
'<div class="notice notice-error"><p>Notice for %s: %s</p><p><a href = "%s%sdd_remove_notice">Remove notice</a></p></div>',
get_user_meta(get_option('dd_notice_userid'), 'billing_email', true),
get_option('dd_notice'),
$currentUrl,
$queryChar
);
}
}
add_action('admin_notices', 'dd_add_notice');
//Remove notice if remove link was clicked
function dd_remove_notice()
{
if (isset($_GET['dd_remove_notice'])) {
delete_option('dd_notice');
}
}
add_action('init', 'dd_remove_notice');