wpdesk/wp-notice

Library for displaying Wordpress notices.

3.2.5 2024-07-23 13:33 UTC

README

pipeline status coverage report Latest Stable Version Total Downloads Latest Unstable Version License

wp-notice

A simple, yet very useful library for WordPress plugins allowing to display the different kind of notices in the admin area. It can be easily used to:

  • Display the simple error, warning, success and info notices,
  • Display the permanently dismissible notices,
  • Handle the dismiss functionality with AJAX requests.

Requirements

PHP 5.5 or later.

Installation via Composer

In order to install the bindings via Composer run the following command:

composer require wpdesk/wp-notice

Next, use the Composer's autoload to use them:

require_once 'vendor/autoload.php';

Manual installation

If you prefer not to use the Composer you can also download the latest library release. Once it is done, simply include the init.php file to use the Notices.

require_once('/path/to/wp-desk/wp-notice/init.php');

Getting Started

Notices usage example

$notice = wpdesk_wp_notice('Notice text goes here');

// Is equivalent to:
$notice = WPDeskWpNotice('Notice text goes here');

// Is equivalent to:
$notice = \WPDesk\Notice\Factory::notice('Notice text goes here');

// Is equivalent to:
$notice = new \WPDesk\Notice\Notice('Notice text goes here'); 

Please mind that the Notice must be used before WordPress admin_notices action. You can find WordPress admin actions order listed here.

Permanently dismissible notices

AJAX handler

In order to use permanently dismissible notices the AJAX handler needs to be created first and the hooks initialized:

wpdesk_init_wp_notice_ajax_handler();

// Is equivalent to:
( new \WPDesk\Notice\AjaxHandler() )->hooks();

Displaying the permanently dismissible notices

Use the following code for the permanently dismissible notice to be displayed:

wpdesk_permanent_dismissible_wp_notice( 'Notice text goes here', 'notice-name' );

// Is equivalent to
$notice = new \WPDesk\Notice\PermanentDismissibleNotice( 'Notice text goes here', 'notice-name' );

Project documentation

PHPDoc: https://wpdesk.gitlab.io/wp-notice/index.html