duckdev/wp-flash-notices

WordPress admin notices as flash notice using transient API to display after page reload.

1.0.1 2019-07-25 13:04 UTC

This package is auto-updated.

Last update: 2024-11-14 16:53:31 UTC


README

WordPress admin notices as flash notice using transient API to display after page reload.

By default WordPress doesn't provide a solution to show admin notices after page reload. Frameworks lik CodeIgniter does have a flash message feature. Using which, you can show one time notices across user interface. With this simple library you can make use of WordPress' transient API and show one time admin notices like a flash message.

Installation

You can install this library using composer

composer require duckdev/wp-flash-notices

Or, you can download latest version of the library from releases tab.

Dependencies

  • PHP 5.4+
  • WordPress 4.0+

Usage Example

Import the library class and assign custom namespace.

use DuckDev\WP_Flash_Notices as My_Custom_Notices;

Create new instance of the notices class with our custom transient name.

$notices = new My_Custom_Notices( 'my-custom-notices' );

Notice Types

There are 4 types of notices.

Success Notice:

Notice with green bar.

// Register new success notice to the queue.
$notices->add( 'custom-success', 'This is a custom success notice.', 'success' );

Error Notice:

Notice with red bar.

// Register new error notice to the queue.
$notices->add( 'custom-error', 'This is a custom error notice.', 'error' );

Warning Notice:

Notice with yellow bar.

// Register new warning notice to the queue.
$notices->add( 'custom-warning', 'This is a custom warning notice.', 'warning' );

Info Notice:

Notice with blue bar.

// Register new info notice to the queue.
$notices->add( 'custom-info', 'This is a custom info notice.', 'info' );

Dismissible Notice

Show a dismiss button in notice.

// Register new info notice to the queue.
$notices->add( 'custom-success', 'This is a custom dismissible notice.', 'success', true );

Network Admin Notice

By default all notices are shown within single site admin screens. If you have Multisite you can add network admin notices by setting last argument as true:

// Register new info notice to the queue.
$notices->add( 'custom-success', 'This is a custom dismissible notice.', 'success', true, true );

Frontend Compatibility

If you'd like to print the notices in the front-end, then use the below action to your template.

do_action( 'front_notices' );