pressmodo/wp-admin-notices

一个用于创建持久和可 dismiss 的 WordPress 管理通知的辅助库。

1.1.3 2020-05-15 15:22 UTC

This package is auto-updated.

Last update: 2024-09-16 01:07:37 UTC


README

一个用于创建持久和可 dismiss 的 WordPress 管理通知的辅助库。

使用此方法创建的通知将自动可 dismiss。

使用方法

$my_notices = new \Pressmodo\AdminNotices\Notices();

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

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

使用以下代码实例化 Notices 对象后,您可以使用 add() 方法添加新的通知: $my_notices = new \Pressmodo\AdminNotices\Notices();

此方法的参数如下

$options 参数是一个数组,可以包含以下可选项目

示例

您可以在主题现有代码中添加以下代码。

首先,我们需要实例化 Notices 对象

use Pressmodo\AdminNotices\Notices;

$my_notices = new Notices();

添加一个简单、默认的通知

$my_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.
);

上面的示例将创建一个新的通知,该通知将仅在所有仪表板页面上显示。当通知被 dismiss 时,将在数据库中保存一个新的选项,其键为 pressmodo_notice_dismissed_my_theme_notice。键通过将 $id 添加到选项的默认前缀(pressmodo_notice_dismissed),用下划线分隔来创建。

添加一个更定制化的通知

$my_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.
    ]
);

上面的示例将创建一个新的通知,该通知仅在仪表板的“主题”屏幕上显示。当通知被 dismiss 时,将保存一个新的用户元数据,存储的用户元数据键为 my_theme_my_notice。键通过将 $id 添加到我们定义的 option_prefix,用下划线分隔来创建。

Notices 类可以用于添加多个通知。添加完通知后,您将必须运行 boot 方法,以便将通知添加到仪表板

$my_notices->boot();

总结上述内容,添加管理通知的完整示例如下

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

自动加载

您需要使用自动加载器。理想情况下,这将是一个 Composer

Composer

从命令行

composer require pressmodo/wp-admin-notices