# WPTRT Admin Notices
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
```php
$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](https://wordpress.org/support/article/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](https://codex.wordpress.org/Plugin_API/Admin_Screen_Reference). | `[]` |
| `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:
```php
use WPTRT\AdminNotices\Notices;
$my_theme_notices = new Notices();
```
To add a simple, default notice:
```php
$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:
```php
$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:
```php
$my_theme_notices->boot();
```
To sum up all the above, a complete example of how to add an admin notice would look like this:
```php
$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](https://getcomposer.org). However, we have a [basic autoloader](https://github.com/WPTRT/autoload) available to include with themes if needed.
### Composer
From the command line:
```sh
composer require wptrt/admin-notices
```
### WPTRT Autoloader
If using the WPTRT autoloader, use the following code:
```php
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();
```
没有合适的资源?快使用搜索试试~ 我知道了~
【WordPress插件】2022年最新版完整功能demo+插件v1.7.2.zip
共329个文件
php:265个
js:27个
css:10个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 42 浏览量
2022-04-15
06:53:22
上传
评论
收藏 738KB ZIP 举报
温馨提示
"【WordPress插件】2022年最新版完整功能demo+插件v1.7.2 Barn2 Media WooCommerce Wholesale Pro Barn2 Media Woocommerce Wholesale Pro" ---------- 泰森云每天更新发布最新WordPress主题、HTML主题、WordPress插件、shopify主题、opencart主题、PHP项目源码、安卓项目源码、ios项目源码,更有超10000个资源可供选择,如有需要请站内联系。
资源推荐
资源详情
资源评论
收起资源包目录
【WordPress插件】2022年最新版完整功能demo+插件v1.7.2.zip (329个子文件)
main.css 33KB
plugin-promo.css 3KB
plugin-promo.min.css 2KB
wcwp-admin-wc-settings.min.css 2KB
wc-settings.css 1KB
wc-settings.min.css 999B
wcwp-admin-product-category.min.css 861B
wcwp-admin-edit-user.min.css 237B
wcwp-admin-wc-settings-shipping-payment.min.css 185B
wcwp-admin-user-list.min.css 148B
main.js 1.03MB
wizard.min.js 101KB
layoutFull.js 14KB
jquery.tipTip.js 7KB
jquery.tipTip.min.js 3KB
wcwp-admin-edit-user.js 3KB
wcwp-admin-new-user.js 3KB
index.js 2KB
wcwp-admin-product-category.js 2KB
wcwp-admin-new-user.min.js 1KB
wcwp-admin-edit-user.min.js 1KB
wcwp-admin-wc-settings.js 1KB
wcwp-admin-role-sorting.js 1KB
wcwp-admin-registration-fields.js 866B
wcwp-admin-product-category.min.js 843B
wcwp-admin-role-sorting.min.js 580B
wcwp-admin-registration-fields.min.js 559B
barn2-notices.js 549B
barn2-notices.js 549B
wcwp-admin-wc-settings.min.js 541B
wcwp-admin-shipping-payment-roles.js 466B
barn2-notices.min.js 296B
barn2-notices.min.js 296B
wcwp-admin-shipping-payment-roles.min.js 261B
wc-settings.js 195B
wc-settings.min.js 124B
webpack.config.js 76B
installed.json 26KB
wp-dependencies.json 1KB
composer.json 1KB
LICENSE 18KB
LICENSE 1KB
yarn.lock 305KB
composer.lock 24KB
readme.md 6KB
woocommerce-wholesale-pro-es_ES.mo 35KB
Wholesale_Store.php 24KB
Setup_Wizard.php 21KB
Plugin_License.php 20KB
Layered_Nav.php 16KB
ClassLoader.php 14KB
InstalledVersions.php 14KB
ClassLoader.php 14KB
Util.php 14KB
Category_Edit.php 14KB
Custom_Settings_Fields.php 13KB
Login_Register.php 12KB
Util.php 11KB
General.php 11KB
WCWP_Email.php 11KB
Roles.php 10KB
class-html-data-table.php 10KB
Category_Protector.php 10KB
License_Key_Setting.php 10KB
Installer.php 9KB
Price_Handler.php 9KB
Wholesale_Roles.php 9KB
Plugin.php 8KB
License_Notices.php 7KB
Wholesale_Role.php 7KB
Admin_New_User.php 7KB
Product_Table.php 7KB
InstalledVersions.php 7KB
Plugin_Updater.php 7KB
Customer_New_Account.php 7KB
Notice.php 7KB
Admin.php 7KB
Price_Filter.php 7KB
Settings_API_Helper.php 6KB
User_List.php 6KB
Step.php 6KB
EDD_Licensing.php 6KB
class-wc-settings-additional-field-types.php 6KB
Util.php 6KB
Category_Visibility.php 6KB
WCWP_Widget.php 6KB
wholesale-login-register-form.php 6KB
Registration_Fields.php 6KB
User_Edit.php 6KB
Customer_Registration_Approved.php 5KB
Settings_Page.php 5KB
Emails.php 5KB
Payment_Roles.php 5KB
Product_Addons.php 5KB
Plugin_Promo.php 5KB
Shipping_Roles.php 5KB
Layered_Nav_Filters.php 5KB
Layout.php 5KB
Rating_Filter.php 5KB
Navigation_Menu.php 5KB
共 329 条
- 1
- 2
- 3
- 4
资源评论
Lee达森
- 粉丝: 968
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功