Development, Theme, Tutorials

How to Add Admin Alerts and Error Messages to the Backend of WordPress

shutterstock_258805193

Admin notices help you to either prompt your users to act on something, or simply alert them about something that just happened, such as an error or success of an action. Read on to learn how you can easily add admin alerts to the backend of WordPress.

How to Add Notices

You can use an action to add update messages to your page. Actions enable you to suppress some messages. To add a notice to the page, use the admin_notices hook. The hooked function echoes the desired HTML, as seen in the example below.

function my_error_notice() {

?>

<?php

}

add_action( ‘admin_notices’, ‘my_error_notice’ );

Adding Dismissible Notices

WordPress 4.2 comes with in-built dismissible alerts. All you have to do on the front-end is give your alerts the is-dismissible classification. However, there is no standard way of remembering the dismissal. To overcome this, you can bind an action to the click, and use AJAX to add the dismissible actions to your page.

To accomplish this, have the option my-acf-notice-dismissed, which would be 1 in case the user dismissed the alert. Detect a click on the dismissal button, and launch an AJAX call – which updates the my-acf-notice-dismissed option to 1. At this point, you can discover the presence of the the_field() ACF function, and eliminate the option.

Admin notices enhance your admin experience. While WordPress 4.2 allows you to dismiss alerts, more work is required to ensure their persistence.

Leave a Reply

Your email address will not be published. Required fields are marked *