wptrt/admin-notices
Composer 安装命令:
composer require wptrt/admin-notices
包简介
A class to create admin-notices for the WordPress dashboard.
关键字:
README 文档
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:
| Parameter | Type | Description | |
|---|---|---|---|
$id |
string |
Required | A unique ID for this notice. The ID can contain lowercase latin letters and underscores. It is used to construct the option (or user-meta) key that will be strored in the database. |
$title |
string |
Required | The title for your notice. If you don't want to use a title you can use set it to false. |
$message |
string |
Required | The content for the notice you want to create. Please note that the only acceptable tags here are <p>, <a>, <em>, <strong>. |
$options |
array |
Optional | Extra arguments for this notice. Can be used to alter the notice's default behavior. |
The $options argument is an array that can have the following optional items:
| Key | Type | Value | Default |
|---|---|---|---|
scope |
string |
Can be global or user. Determines if the dismissed status will be saved as an option or user-meta. |
global |
type |
string |
Can be one of info, success, warning, error. |
info |
alt_style |
bool |
Set to true if you want to use alternative styles. These have a background-color depending on the type argument - in contrast to the normal styles that use a white background. |
false |
capability |
string |
The user capability required to see the notice. For a list of all available capabilities please refer to the Roles and Capabilities article. | edit_theme_options |
screens |
array |
An array of screens where the notice will be displayed. For a reference of all available screen-IDs, refer to this article. | [] |
option_prefix |
string |
The prefix that will be used to build the option (or user-meta) name. Can contain lowercase latin letters and underscores. The actual option is built by combining the option_prefix argument with the defined ID from the 1st argument of the add() method. |
wptrt_notice_dismissed |
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();
wptrt/admin-notices 适用场景与选型建议
wptrt/admin-notices 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 57.32k 次下载、GitHub Stars 达 91, 最近一次更新时间为 2019 年 09 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wptrt/admin-notices 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wptrt/admin-notices 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wptrt/admin-notices 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Must-use plugin integrating WordPress with the Upsun platform: environment awareness, router-cache friendliness, safe preview clones, deploy migrations, Site Health checks, and a wp upsun CLI command.
Inertia.js server-side adapter for PHP. Handles full visits, Inertia XHR visits, asset-version 409 handshakes and partial reloads. Framework-agnostic, with transparent WordPress integration.
Adds Cache-Tag HTTP response headers for singular WordPress content and purges Cloudflare by tag when content changes.
AI Chatbot & Helpdesk Agent for WooCommerce. Connects your store to Egentify for AI-powered chat, order tracking, email, and ticketing.
MIDDAG's reference architecture for a WordPress plugin — composes the OSS framework/wordpress/ui libraries end to end.
Assets helper package for the Hybrid Core framework.
统计信息
- 总下载量: 57.32k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 92
- 点击次数: 16
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2019-09-07