Download the PHP package stellarwp/admin-notice without Composer

On this page you can find all versions of the php package stellarwp/admin-notice. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package admin-notice

StellarWP Admin Notice

CI Pipeline

This library exposes an object-oriented interface around WordPress' admin notices.

Why is this useful?

Normally, WordPress plugins have to resort to manually constructing admin notices:

Unfortunately, this starts getting really messy if you want to conditionally add classes (e.g. .inline, .notice-alt, etc.) or check user capabilities. Furthermore, WordPress doesn't provide a consistent way to track whether or not a particular notice has been dismissed, forcing each plugin to come up with its own solution.

This library sets out to solve that problem, giving you a fluent API for constructing and rendering admin notices in your plugins:

With just a few lines of code, you have a dismissible, info-level notice that will get displayed automatically on "admin_notices", but only if the current user has the "manage_options" capability!

Installation

It's strongly recommended that you install this library as a project dependency via Composer:

In order to prevent conflicts between your code and other plugins or themes that might implement this library, it's strongly recommended that you take advantage of PHP Autoloading and do not include_once or require_once these files!

Usage

The most basic version of the AdminNotice class is as follows:

💡 To make it easier to construct notices fluently, you may also use the static AdminNotice::factory() method, which accepts the same arguments as new AdminNotice().

This $notice object represents an info-level notice, whose markup will look something like this:

There are two ways of getting the markup for this notice:

  1. $notice->render(): This method return the markup as a string.
  2. $notice->display(): This method will print the markup to the screen, and is essentially an alias of echo $notice->render();

💡 The AdminNotice class implements the __toString() magic method, so you may also write something like echo $notice.

Admin notice levels

WordPress supports four levels of admin notices:

  1. success (green color scheme)
  2. warning (yellow color scheme)
  3. error (red color scheme)
  4. info (blue color scheme)

For your convenience, the following constants are available on the AdminNotice class, to be passed to the second argument of the class constructor:

Rendering admin notices

Now, we need a way to render the notice, typically during the "admin_notices" action.

Admin notices may be queued in a few different ways:

  1. Explicitly call add_action() with the notice's display() method as the callback:

  2. Call the queue() method on the notice itself:

  3. Write your own handler that may work with the notice instance.

Remembering dismissed notices

Often, it can be helpful to remember if a user has dismissed a particular notice. For this reason, AdminNotice::setDismissible() accepts two arguments:

  1. Whether or not a notice may be dismissed.
  2. An optional key for tracking dismissals.

If a dismissible admin notice with a key is rendered, AdminNotice will automatically enqueue JavaScript to fire an AJAX request when such notices are dismissed: once a user dismisses the notice, the key and timestamp are stored in their user meta.

The only thing the AdminNotice can't do itself is queue up the AJAX handler. Instead, it's necessary to call StellarWP\AdminNotice\DismissalHandler::listen() somewhere in your plugin's setup process (wherever you choose to register your hooks).

Example

This example will display the notice with key "some-unique-key" until the user dismisses it.

Checking user capabilities

Often, admin notices are only meant for a small sub-set of users. For example, there isn't much reason to tell all users about a new, premium extension for your plugin if only administrator-level users can install plugins.

For this reason, the AdminNotice class lets notices be wrapped in a current_user_can() check:

The full AdminNotice API

These are all of the methods available for constructing your admin notices:

__construct(string $message[, string $type = 'info'])

Construct a new AdminNotice instance.

Arguments

string $message
The body of the admin notice.
This may contain HTML, and will be run through wpautop() and wp_kses_post() prior to rendering.
string $type
Optional. The type of notice, one of "success", "error", "warning", or "info". Default is "info".

See also: AdminNotice::factory()

dismissedByUser(?int $user = null): bool

Check to see if this notice has been dismissed by the given user. Will return true if the user has previously dismissed this notice, false otherwise.

Arguments

?int $user
Optional. The user ID. Default is null (the current user).

dismissedByUserAt(?int $user = null): DateTimeImmutable

Retrieve a DateTime object representing when the given user [last] dismissed this notice.

The method will return a DateTimeImmutable object representing when the notice was dismissed, or NULL if the user has not dismissed this notice.

Arguments

?int $user
Optional. The user ID. Default is null (the current user).

dismissForUser(?int $user = null): self

Mark a dismissible notice as dismissed by the given user.

This behaves the same as the static dismissNoticeForUser() method, but doesn't need to be given the notice key (and won't try to dismiss one if no key is present).

Arguments

?int $user
Optional. The user ID. Default is null (the current user).

display(): void

Render and print the admin notice.

queue([int $priority = 10]): self

Queue this admin notice to be displayed on "admin_notices".

Arguments

int $priority
Optional. The priority to use with add_action(). Default is 10.

render(): string

Render the admin notice.

This method is responsible for all of the logic around how the notice's markup gets built, including whether or not it should show anything at all (based on things like dismissal history).

setAlt(bool $alt): self

Set whether or not WordPress should use alternate coloring for this notice.

Arguments

bool $alt
True if alternate coloring should be used, false otherwise.

setCapability([string $capability = null]): self

Define a capability check that must be satisfied before rendering this notice.

Arguments

?string $capability
A capability that the current user must possess. Passing NULL will remove any existing capability requirements.

setDismissible(bool $dismissible[, ?string $key = null]): self

Set whether or not this notice should be dismissible by the user.

Arguments

bool $dismissible
Whether or not the notice may be dismissed by the user.
?string $key = null
Optional. A unique key identifying this notice. Default is null (do not track dismissal).
Once a user has dismissed a notice with this ID, future notices with the same ID will not be rendered.

setInline(bool $inline): self

Specify whether or not a notice should be rendered inline or pulled to the top of the page (default).

Arguments

bool $inline
True if the notice should be rendered inline, false otherwise.

static dismissNoticeForUser(string $notice[, ?int = null]): bool

Mark an individual notice as dismissed for the given user ID.

Will return true if the notice has been added to the user's list of dismissed notices, false otherwise.

Arguments

string $notice
The admin notice's dismissible key.
int $user
Optional. The user ID. Default is null (the current user).

static factory(string $message[, string $type = 'info']): AdminNotice

This method is an alias for the class constructor (and accepts the same arguments), making it easier to write fluent strings.

Both notices in the following are equivalent:

Contributing

If you're interested in contributing to the project, please see our contributing documentation.

License

This library is licensed under the terms of the MIT license.


All versions of admin-notice with dependencies

PHP Build Version
Package Version
Requires php Version ^5.6 | ^7.0 | ^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package stellarwp/admin-notice contains the following files

Loading the files please wait ....