PHPackages                             stellarwp/admin-notices - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. stellarwp/admin-notices

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

stellarwp/admin-notices
=======================

A handy package for easily displaying admin notices in WordPress with simple to complex visibility conditions

2.0.1(10mo ago)12638.9k↑38.7%[3 issues](https://github.com/stellarwp/admin-notices/issues)MITPHPCI passing

Since Oct 14Pushed 10mo ago4 watchersCompare

[ Source](https://github.com/stellarwp/admin-notices)[ Packagist](https://packagist.org/packages/stellarwp/admin-notices)[ RSS](/packages/stellarwp-admin-notices/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (12)Versions (11)Used By (0)

Introduction
============

[](#introduction)

Displaying notices within the WordPress admin is a highly common need in plugins. Displaying notices is not difficult, but it gets tedious when wanting to conditionally display notices based on conditions such as user capability, the current screen, date range, and so forth.

This library is intended to provide a simple, readable way for developers to conditionally display standard or highly customized notices within the WordPress admin.

How to use
==========

[](#how-to-use)

Installation
------------

[](#installation)

It's recommended that you install Admin Notices as a project dependency via [Composer](https://getcomposer.org/):

```
composer require stellarwp/admin-notices
```

> We *actually* recommend that this library gets included in your project using [Strauss](https://github.com/BrianHenryIE/strauss).
>
> Luckily, adding Strauss to your `composer.json` is only slightly more complicated than adding a typical dependency, so checkout our [strauss docs](https://github.com/stellarwp/global-docs/blob/main/docs/strauss-setup.md).

Configuration &amp; Initialization
----------------------------------

[](#configuration--initialization)

The `AdminNotices` class can be used to configure the library to work within your system and avoid conflicts with other plugins. Here's an example of what typical setup may look like:

```
use StellarWP\AdminNotices\AdminNotices;

AdminNotices::initialize('my_plugin', plugin_dir_url(__FILE__) . 'vendor/stellarwp/admin-notices');
```

The `initialize` method accepts two arguments:

1. A unique identifier for your plugin. This is used to avoid conflicts with other plugins.
2. The URL to the library's assets directory. This is used to enqueue the necessary JS files.

### Service Containers

[](#service-containers)

It is not required to use a service container with this library, however if you are using one and want it to fit within your system, you can connect your container, which **must** implement the `Psr\Container\ContainerInterface` interface.

Once connected, your container must provide a concrete instance of the `StellarWP\AdminNotices\Contracts\NotificationsRegistrarInterface` interface. You can either bind the `StellarWP\AdminNotices\NotificationsRegistrar` class to the interface, or create your own class that implements the interface.

```
$container->set('StellarWP\AdminNotices\Contracts\NotificationsRegistrarInterface', function () {
    return new StellarWP\AdminNotices\NotificationsRegistrar();
});

AdminNotices::setContainer($container);
```

Displaying Notices
------------------

[](#displaying-notices)

All notices are displayed using the `StellarWP\AdminNotices\AdminNotices` facade. There are a few methods to manage notices:

### `addNotice($id, $message)`

[](#addnoticeid-message)

Adds a notice to the queue to be displayed in the standard WordPress admin notice area.

Parameters:

1. `string $id` - A unique identifier for the notice.
2. `string|callback $message` - The message to display. This can be a string or a callback that returns a string. The callback receives an instance of the `AdminNotice` class and an instance of the [NoticeElementProperties](src/DataTransferObjects/NoticeElementProperties.php) class — the latter of which is useful for custom notices.

```
use StellarWP\AdminNotices\AdminNotices;
use StellarWP\AdminNotices\AdminNotice;
use \StellarWP\AdminNotices\DataTransferObjects\NoticeElementProperties;

AdminNotices::show('my_notice', 'This is a notice');
AdminNotices::show('my_notice', function (AdminNotice $notice, NoticeElementProperties $elements) {
    return 'This is a notice';
});
```

### `removeNotice($id)`

[](#removenoticeid)

Removes a notice from the queue.

Parameters:

1. `string $id` - The unique identifier for the notice to remove

```
use StellarWP\AdminNotices\AdminNotices;

AdminNotices::removeNotice('my_notice');
```

### `render(AdminNotice $notice)`

[](#renderadminnotice-notice)

Immediately renders the notice to the screen. This is useful if you want to display the notice in a non-standard location.

Parameters:

1. `AdminNotice $notice` - The notice to render

```
use StellarWP\AdminNotices\AdminNotices;

$notice = new AdminNotice('my_notice', 'This is a notice');
AdminNotices::render($notice);
```

Notice Conditions
-----------------

[](#notice-conditions)

At the core of this library is the `AdminNotice` class. This class is used to define the notice and its conditions. When the `AdminNotices::show()` method is used, it returns a new instance of the `AdminNotice` class to be fluently configured. For example:

```
use StellarWP\AdminNotices\AdminNotices;

$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->on('edit.php');
    ->ifUserCan('manage_options')
    ->dismissible()
```

### `on(...$screen)`

[](#onscreen)

Sets the screen where the notice should be displayed. This can be one of:

- A string representing a portion of the URL
- A regex delimited with ~ to compare against the URL (e.g. `~edit\.php~i`)
- An associative array that is compared against the `WP_Screen` object (e.g. `['id' => 'edit-post']`)

If multiple screen conditions are provided, the notice will be displayed if any of the conditions are met.

Parameters:

1. `string|array $screen` - The screen to display the notice on

```
use StellarWP\AdminNotices\AdminNotices;

// Display the notice where the URL contains 'edit.php'
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->on('edit.php');

// Display the notice where the URL matches the regex
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->on('~edit\.php~i');

// Display the notice on the 'edit-post' screen
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->on(['id' => 'edit-post']);
```

### `ifUserCan(...$capability)`

[](#ifusercancapability)

Sets the capability required to view the notice. This can be a single capability or an array of capabilities. Under the hood, `current_user_can` is used to check the capability. Each capability can be one of:

- A string representing a capability
- An array where the elements are spread to the `current_user_can` function
- An instance of the `StellarWP\AdminNotices\ValueObjects\UserCapability` class

If multiple capabilities are provided, the notice will be displayed if the user has any of the capabilities.

Parameters:

1. `string|array|UserCapability $capability` - The capability required to view the notice

```
use StellarWP\AdminNotices\AdminNotices;

// Display the notice if the user can manage options
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->ifUserCan('manage_options');

// Display the notice if the user can manage options or edit posts
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->ifUserCan('manage_options', 'edit_posts');

// Display the notice if the user can edit post 1
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->ifUserCan(['edit_post', 1]);

// Display the notice via a UserCapability object
$capability = new StellarWP\AdminNotices\ValueObjects\UserCapability('manage_options');
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->ifUserCan($capability);
```

### `after($date)`

[](#afterdate)

Sets the date after which the notice should be displayed.

Parameters:

1. `string $date` - The date after which the notice should be displayed.

```
use StellarWP\AdminNotices\AdminNotices;

// Display the notice after January 1, 2022, using a date parsable string
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->after('2022-01-01');

// Display the notice after January 1, 2022, using a DateTime object
$date = new DateTime('2022-01-01');
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->after($date);

// Display the notice after January 1, 2022, using a timestamp
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->after(1640995200);
```

### `until($date)`

[](#untildate)

Sets the date until which the notice should be displayed.

Parameters:

1. `string $date` - The date until which the notice should be displayed.

```
use StellarWP\AdminNotices\AdminNotices;

// Display the notice until January 1, 2022, using a date parsable string
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->until('2022-01-01');

// Display the notice until January 1, 2022, using a DateTime object
$date = new DateTime('2022-01-01');
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->until($date);

// Display the notice until January 1, 2022, using a timestamp
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->until(1640995200);
```

### `between($start, $end)`

[](#betweenstart-end)

Sets the date range during which the notice should be displayed. The dates can be the same string, int, or DateTime object as the `after` and `until` methods.

Parameters:

1. `string $start` - The start date of the range.
2. `string $end` - The end date of the range.

```
use StellarWP\AdminNotices\AdminNotices;

// Display the notice between January 1, 2022, and January 31, 2022, using date parsable strings
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->between('2022-01-01 00:00:00', '2022-01-31 23:59:59');
```

### `when($callback)`

[](#whencallback)

Sets a custom condition for when the notice should be displayed. The callback should return a boolean value.

Parameters:

1. `callable $callback` - The callback that returns a boolean value

```
use StellarWP\AdminNotices\AdminNotices;

// Display the notice if the current user is an administrator
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->when(function () {
        $user = wp_get_current_user();
        return in_array('administrator', $user->roles);
    });
```

Standard Notice Visual &amp; behavior options
---------------------------------------------

[](#standard-notice-visual--behavior-options)

### `autoParagraph($autoParagraph)`, `withoutAutoParagraph()`

[](#autoparagraphautoparagraph-withoutautoparagraph)

**Default:** false

Sets whether the notice message should be automatically wrapped in a paragraph tag. It uses wpautop under the hood.

Parameters:

1. `bool $autoParagraph = true` - Whether to automatically wrap the message in a paragraph tag

```
use StellarWP\AdminNotices\AdminNotices;

// Automatically wrap the message in a paragraph tag
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->autoParagraph();

// Do not automatically wrap the message in a paragraph tag
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->autoParagraph(false);

// Also has an alias for readability
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->withoutAutoParagraph();
```

### `urgency($urgency)`

[](#urgencyurgency)

**Default:** 'info'

Sets the urgency of the notice. This is used to determine the color of the notice. **Only works when the wrapper is enabled.**

Parameters:

1. `string $urgency` - The urgency of the notice. Can be 'info', 'success', 'warning', or 'error'

```
use StellarWP\AdminNotices\AdminNotices;

// Set the notice urgency to 'success'
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->urgency('success');

// The StellarWP\AdminNotices\ValueObjects\Urgency class can also be used
$urgency = new StellarWP\AdminNotices\ValueObjects\Urgency('success');
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->urgency($urgency);
```

### `alternateStyles($useAlternate)`, `standardStyles()`

[](#alternatestylesusealternate-standardstyles)

**Default:** false

Sets whether the notice should use the alternate WordPress notice styles. **Only works when the wrapper is enabled.**

Parameters:

1. `bool $useAlternate = true` - Whether the notice should use the alternate WordPress notice styles

```
use StellarWP\AdminNotices\AdminNotices;

// Use the alternate WordPress notice styles
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->alternateStyles();

// Use the standard WordPress notice styles, only necessary to revert back
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->alternateStyles()
    ->standardStyles();
```

### `inline($inline)`, `notInline()`

[](#inlineinline-notinline)

**Default:** false

Sets whether the notice should be displayed in the WP "inline" location, at the top of the admin page. **Only works when the wrapper is enabled.**

Parameters:

1. `bool $inline = true` - Whether the notice should be displayed inline

```
use StellarWP\AdminNotices\AdminNotices;

// Display the notice inline
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->inline();

// Display the notice in the standard location
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->inline(false);

// Also has an alias for readability
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->notInline();
```

### `dismissible($dismissible)`, `notDismissible()`

[](#dismissibledismissible-notdismissible)

**Default:** false

Sets whether the notice should be dismissible. This adds a dismiss button to the notice. When the user dismisses the notice, it is permanently dismissed. This is stored in the user's preference meta. **Only works when the wrapper is enabled.**

Parameters:

1. `bool $dismissible = true` - Whether the notice should be dismissible

```
use StellarWP\AdminNotices\AdminNotices;

// Make the notice dismissible
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->dismissible();

// Make the notice not dismissible
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->dismissible(false);

// Also has an alias for readability
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->notDismissible();
```

Custom Notices
--------------

[](#custom-notices)

Sometimes you want to display a notice, but you want to completely style it yourself. This is possible and pretty straightforward to do.

Start by using the `custom` method on the notice. This will disable all standard visual and behavior

```
use StellarWP\AdminNotices\AdminNotices;

$notice = AdminNotices::show('my_notice_custom', 'This is a notice')
    ->custom();
```

### `location()`

[](#location)

***Default:*** 'standard'

Sets the location in one of the standard WordPress notice locations. By default, the notice will be displayed in the standard location.

Parameters:

1. `string $location` - The location to display the notice. Can be 'standard', 'above\_header', ' below\_header', or 'inline'. Note that 'standard' and 'below\_header' are the same location.

```
use StellarWP\AdminNotices\AdminNotices;

// Display the notice above the header
$notice = AdminNotices::show('my_notice_custom', 'This is a notice')
    ->custom()
    ->location('above_header');
```

### Dismissing

[](#dismissing)

The `dismissible` method is not available for custom notices. If you want to add a dismiss button, you will need to do so manually. Fortunately, there is a simple way to do this.

```
use StellarWP\AdminNotices\AdminNotices;
use StellarWP\AdminNotices\AdminNotice;
use \StellarWP\AdminNotices\DataTransferObjects\NoticeElementProperties;

$renderCallback = function (AdminNotice $notice, NoticeElementProperties $elements) {
    return "

            This is a custom notice
            closeAttributes()}>
                Dismiss this notice.

    ";
};

AdminNotices::show('my_notice', $renderCallback)
    ->custom();
```

The [$elements](src/DataTransferObjects/NoticeElementProperties.php) object provides a `customCloserAttributes` method that returns the necessary attributes to be place on the dismiss button. This method will add the necessary attributes to dismiss the notice when clicked. This will permanently dismiss the notice, and fade out the notice, similar to the standard WordPress dismissible notices.

If you want the notice to be marked as dismissed, but not fade out, you can pass "clear" to the `customerCloserAttributes` method: e.g. `$elements->customCloserAttributes('clear')`.

Custom scripts &amp; styles
---------------------------

[](#custom-scripts--styles)

Often times, especially for custom notices, you may want to enqueue custom scripts and styles, however they should only be enqueued when the notice is displayed. This can be done by using the `enqueueScript` and `enqueueStyle` methods on the notice.

### `enqueueScript($src, $deps = [], $ver = false, $args = [])`

[](#enqueuescriptsrc-deps---ver--false-args--)

Enqueues a script to be loaded when the notice is displayed, following the same parameters as `wp_enqueue_script`. The only difference is that the loading strategy is "defer" by default.

```
use StellarWP\AdminNotices\AdminNotices;

$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->enqueueScript('https://example.com/my-script.js', ['jquery']);
```

### `enqueueStyle($src, $deps = [], $ver = false, $media = 'all')`

[](#enqueuestylesrc-deps---ver--false-media--all)

Enqueues a style to be loaded when the notice is displayed, following the same parameters as `wp_enqueue_style`.

```
use StellarWP\AdminNotices\AdminNotices;

$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->enqueueStylesheet('https://example.com/my-style.css');
```

Resetting dismissed notices
---------------------------

[](#resetting-dismissed-notices)

For dismissible notices, when the user dismisses the notice, it is permanently dismissed. If you want to reset the dismissed notice(s), there are a couple methods available.

### `resetNoticeForUser($notificationId, $userId)`

[](#resetnoticeforusernotificationid-userid)

Reset a specific notification for a user.

Parameters:

1. `string $notificationId` - The unique identifier for the notice
2. `int $userId` - The user ID to reset the notice for

```
use StellarWP\AdminNotices\AdminNotices;

AdminNotices::resetNoticeForUser('my_notice', get_current_user_id());
```

### `resetAllNoticesForUser($userId)`

[](#resetallnoticesforuseruserid)

Reset all dismissed notices for a user.

Parameters:

1. `int $userId` - The user ID to reset all notices for

```
use StellarWP\AdminNotices\AdminNotices;

AdminNotices::resetAllNoticesForUser(get_current_user_id());
```

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance49

Moderate activity, may be stable

Popularity43

Moderate usage in the ecosystem

Community18

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 81.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~38 days

Recently: every ~66 days

Total

8

Last Release

312d ago

Major Versions

1.2.2 → 2.0.02024-11-20

### Community

Maintainers

![](https://www.gravatar.com/avatar/e3171496e198834c638911ad3c3220bec77871cdf6557eaa9e2a58d16600a1a1?d=identicon)[bordoni](/maintainers/bordoni)

![](https://www.gravatar.com/avatar/70a2847a265444714b48c64eceb3ca742baa3a56757ce65b18bd7bbbbf910312?d=identicon)[dpanta94](/maintainers/dpanta94)

![](https://www.gravatar.com/avatar/97fd764aa710e8d8263a7e3b3fececdfd736b8aad8055227bf592ddf50ad15ba?d=identicon)[stellarwp](/maintainers/stellarwp)

![](https://www.gravatar.com/avatar/ec4ef25906386b60f5f66b3d03064e3bb700bedc94ee8558918565612c74aef3?d=identicon)[johnhooks](/maintainers/johnhooks)

---

Top Contributors

[![JasonTheAdams](https://avatars.githubusercontent.com/u/2024145?v=4)](https://github.com/JasonTheAdams "JasonTheAdams (31 commits)")[![borkweb](https://avatars.githubusercontent.com/u/430385?v=4)](https://github.com/borkweb "borkweb (2 commits)")[![d4mation](https://avatars.githubusercontent.com/u/7770631?v=4)](https://github.com/d4mation "d4mation (1 commits)")[![defunctl](https://avatars.githubusercontent.com/u/1066195?v=4)](https://github.com/defunctl "defunctl (1 commits)")[![JoshuaHungDinh](https://avatars.githubusercontent.com/u/75056371?v=4)](https://github.com/JoshuaHungDinh "JoshuaHungDinh (1 commits)")[![maurisrx](https://avatars.githubusercontent.com/u/6677546?v=4)](https://github.com/maurisrx "maurisrx (1 commits)")[![stratease](https://avatars.githubusercontent.com/u/2826045?v=4)](https://github.com/stratease "stratease (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/stellarwp-admin-notices/health.svg)

```
[![Health](https://phpackages.com/badges/stellarwp-admin-notices/health.svg)](https://phpackages.com/packages/stellarwp-admin-notices)
```

###  Alternatives

[symfony/dependency-injection

Allows you to standardize and centralize the way objects are constructed in your application

4.2k431.1M7.5k](/packages/symfony-dependency-injection)[illuminate/contracts

The Illuminate Contracts package.

705122.9M10.1k](/packages/illuminate-contracts)[illuminate/container

The Illuminate Container package.

31178.1M2.0k](/packages/illuminate-container)[ecotone/ecotone

Supporting you in building DDD, CQRS, Event Sourcing applications with ease.

558549.8k17](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k20](/packages/civicrm-civicrm-core)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
