Created
October 13, 2021 00:55
-
-
Save Chofoteddy/f0436cb776dc81f09dc7b2c5889b48cc to your computer and use it in GitHub Desktop.
WP Admin Notice is a simple object use to show user notifications.
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
/** | |
* WP Admin Notice with pure JavaScript | |
* | |
* This adds a dismissible admin notice to the WP admin with pure JavaScript. | |
* No jQuery or other dependencies. The admin notice will have a Dismiss icon | |
* just like the regular WP admin notices. When the icon is clicked, the message | |
* will go away. | |
* | |
* This script is and Object-Oriented implementation of @Isabelc post (see link below). | |
* | |
* @see {@link https://isabelcastillo.com/wp-admin-notice-javascript Show WP Admin Notice With Pure JavaScript} | |
* @see {@link https://developer.wordpress.org/reference/hooks/admin_notices Admin Notices - WP Code Reference} | |
*/ | |
let Notification = Object.create(null); | |
/** | |
* Define the notice level constants: | |
* | |
* - error: will display the message with a white background and a red left border. | |
* - warning: will display the message with a white background and a yellow/orange left border. | |
* - success: will display the message with a white background and a green left border. | |
* - info: will display the message with a white background a blue left border. | |
*/ | |
Object.defineProperties(Notification, { | |
INFO: { | |
value: 'info', | |
writable: false, | |
enumerable: true, | |
}, | |
SUCCESS: { | |
value: 'success', | |
writable: false, | |
enumerable: true, | |
}, | |
ERROR: { | |
value: 'error', | |
writable: false, | |
enumerable: true, | |
}, | |
WARNING: { | |
value: 'warning', | |
writable: false, | |
enumerable: true, | |
}, | |
}); | |
/** | |
* Shows info message. | |
* | |
* @param msg The message to be shown. | |
*/ | |
Notification.info = function info(msg) { | |
this.msg(msg, Notification.INFO); | |
} | |
/** | |
* Shows success message. | |
* | |
* @param msg The message to be shown. | |
*/ | |
Notification.success = function success(msg) { | |
this.msg(msg, Notification.SUCCESS); | |
} | |
/** | |
* Shows error message. | |
* | |
* @param msg The message to be shown. | |
*/ | |
Notification.error = function error(msg) { | |
this.msg(msg, Notification.ERROR); | |
} | |
/** | |
* Shows warning message. | |
* | |
* @param msg The message to be shown. | |
*/ | |
Notification.warning = function warning(msg) { | |
this.msg(msg, Notification.WARNING); | |
} | |
/** | |
* Create and show a dismissible admin notice | |
*/ | |
Notification.msg = function msg(msg, level) { | |
/* create notice div */ | |
var div = document.createElement('div'); | |
div.classList.add('notice', 'notice-' + level); | |
/* create paragraph element to hold message */ | |
var p = document.createElement('p'); | |
/* Add message text */ | |
p.appendChild(document.createTextNode(msg)); | |
/* Add the whole message to notice div */ | |
div.appendChild(p); | |
/* Create Dismiss icon */ | |
var b = document.createElement('button'); | |
b.setAttribute('type', 'button'); | |
b.classList.add('notice-dismiss'); | |
/* Add screen reader text to Dismiss icon */ | |
var bSpan = document.createElement('span'); | |
bSpan.classList.add('screen-reader-text'); | |
bSpan.appendChild(document.createTextNode('Dismiss this notice')); | |
b.appendChild(bSpan); | |
/* Add Dismiss icon to notice */ | |
div.appendChild(b); | |
/* Insert notice after the first h1 */ | |
var h1 = document.getElementsByTagName('h1')[0]; | |
h1.parentNode.insertBefore(div, h1.nextSibling); | |
/* Make the notice dismissable when the Dismiss icon is clicked */ | |
b.addEventListener('click', function () { | |
div.parentNode.removeChild(div); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment