wptrt/admin-notices

A class to create admin-notices for the WordPress dashboard.

v1.0.4 2023-10-06 03:42 UTC

This package is auto-updated.

Last update: 2024-12-09 13:49:19 UTC


README

This is a custom class allowing WordPress theme authors to add admin notices to the WordPress dashboard. Its primary purpose is for providing a standardized method of creating admin notices in a consistent manner using the default WordPress styles.

Notices created using this method are automatically dismissible.

Usage

$my_theme_notices = new \WPTRT\AdminNotices\Notices();

// Add a notice.
$my_theme_notices->add( (string) $id, (string) $title, (string) $content, (array) $options );

// Boot things up.
$my_theme_notices->boot();

After you instantiate the Notices object using $my_theme_notices = new \WPTRT\AdminNotices\Notices(); you can add new notices using the add() method.

The arguments of this method are:

The $options argument is an array that can have the following optional items:

Examples

You can add the following code within your theme's existing code.

First we need to instantiate the Notices object:

use WPTRT\AdminNotices\Notices;

$my_theme_notices = new Notices();

To add a simple, default notice:

$my_theme_notices->add(
    'my_theme_notice',                           // Unique ID.
    esc_html__( 'Notice Title', 'textdomain' ),  // The title for this notice.
    esc_html__( 'Notice content', 'textdomain' ) // The content for this notice.
);

The above example will create a new notice that will only show on all dashboard pages. When the notice gets dismissed, a new option will be saved in the database with the key wptrt_notice_dismissed_my_theme_notice. The key gets created by appending the $id to the default prefix for the option (wptrt_notice_dismissed), separated by an underscore.

To add a more customized notice:

$my_theme_notices->add(
    'my_notice',                                  // Unique ID.
    esc_html__( 'Notice Title', 'textdomain' ),   // The title for this notice.
    esc_html__( 'Notice content', 'textdomain' ), // The content for this notice.
    [
        'scope'         => 'user',       // Dismiss is per-user instead of global.
        'screens'       => [ 'themes' ], // Only show notice in the "themes" screen.
        'type'          => 'warning',    // Make this a warning (orange color).
        'alt_style'     => true,         // Use alt styles.
        'option_prefix' => 'my_theme',   // Change the user-meta prefix.
    ]
);

The above example will create a new notice that will only show in the "Themes" screen in the dashboard. When the notice gets dismissed, a new user-meta will be saved and the key for the stored user-meta will be my_theme_my_notice. The key gets created by appending the $id to our defined option_prefix, separated by an underscore.

The Notices class can be used to add multiple notices. Once you have finished adding the notices, you will have to run the boot method so that the notices can be added to the dashboard:

$my_theme_notices->boot();

To sum up all the above, a complete example of how to add an admin notice would look like this:

$my_theme_notices = new \WPTRT\AdminNotices\Notices();
$my_theme_notices->add( 'my_theme_notice', __( 'Title', 'textdomain' ), __( 'Content', 'textdomain' ) );
$my_theme_notices->boot();

Autoloading

You'll need to use an autoloader with this. Ideally, this would be Composer. However, we have a basic autoloader available to include with themes if needed.

Composer

From the command line:

composer require wptrt/admin-notices

WPTRT Autoloader

If using the WPTRT autoloader, use the following code:

include get_theme_file_path( 'path/to/autoload/src/Loader.php' );

$loader = new \WPTRT\Autoload\Loader();
$loader->add( 'WPTRT\\AdminNotices\\Notice', get_theme_file_path( 'path/to/admin-notices/src' ) );
$loader->register();