承接 arraypress/wp-register-notices 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

arraypress/wp-register-notices

Composer 安装命令:

composer require arraypress/wp-register-notices

包简介

A declarative WordPress admin notices system with automatic GET parameter handling, conditional display, and dismissible notices

README 文档

README

A declarative, lightweight system for managing WordPress admin notices with automatic GET parameter handling, conditional display, and dismissible notices.

Features

  • Declarative API: Register all your notices in one place with a clean configuration array
  • Automatic Triggers: Notices appear based on GET parameters (e.g., ?updated=1)
  • Dynamic Messages: Support for callbacks with data interpolation
  • Conditional Display: Show notices based on conditions (e.g., missing API keys)
  • Dismissible Notices: Built-in AJAX dismissal with per-user persistence
  • Page Targeting: Limit notices to specific admin pages with wildcard support
  • Smart Type Detection: Automatically infers notice type from key naming
  • Capability Checks: Restrict notices based on user permissions

Installation

Install via Composer:

composer require arraypress/wp-register-notices

Basic Usage

use function ArrayPress\AdminNotices\register_admin_notices;

// Register your notices during plugin initialization
register_admin_notices( 'myplugin', [
    // Simple success messages
    'added'   => __( 'Item added successfully.', 'myplugin' ),
    'updated' => __( 'Item updated successfully.', 'myplugin' ),
    'deleted' => __( 'Item deleted successfully.', 'myplugin' ),
] );

// Trigger by redirecting with the parameter
wp_redirect( admin_url( 'admin.php?page=myplugin&updated=1' ) );

Advanced Examples

Dynamic Messages with Data

register_admin_notices( 'myplugin', [
    'bulk_deleted' => [
        'message' => function( $args ) {
            $count = absint( $args['count'] ?? 0 );
            return sprintf(
                _n( '%d item deleted.', '%d items deleted.', $count, 'myplugin' ),
                $count
            );
        },
        'type' => 'success',
        'data' => ['count']  // Collect these GET parameters
    ]
] );

// Trigger: ?bulk_deleted=1&count=5
// Shows: "5 items deleted."

Persistent Conditional Notices

register_admin_notices( 'myplugin', [
    'api_key_missing' => [
        'condition' => function() {
            return empty( get_option( 'myplugin_api_key' ) );
        },
        'message' => function() {
            $url = admin_url( 'admin.php?page=myplugin-settings' );
            return sprintf( 
                __( 'API key required. <a href="%s">Configure settings</a>.', 'myplugin' ),
                esc_url( $url )
            );
        },
        'type'        => 'warning',
        'persistent'  => true,        // Shows on every page load
        'dismissible' => false         // Can't be dismissed
    ]
] );

Page-Specific Notices

register_admin_notices( 'myplugin', [
    'settings_saved' => __( 'Settings saved successfully.', 'myplugin' ),
    'license_activated' => __( 'License activated.', 'myplugin' ),
], [
    'pages' => ['myplugin-settings', 'myplugin-license'],  // Only these pages
    'capability' => 'manage_options'                        // Admin only
] );

Error Handling

register_admin_notices( 'myplugin', [
    'error' => [
        'message' => function( $args ) {
            return esc_html( $args['message'] ?? __( 'An error occurred.', 'myplugin' ) );
        },
        'type' => 'error',
        'data' => ['message']
    ]
] );

// Trigger: ?error=1&message=Invalid+email+address
// Shows: "Invalid email address"

Working with IDs

register_admin_notices( 'myplugin', [
    'item_updated' => [
        'message' => function( $args ) {
            if ( ! empty( $args['id'] ) ) {
                return sprintf( __( 'Item #%d updated.', 'myplugin' ), $args['id'] );
            }
            return __( 'Item updated.', 'myplugin' );
        },
        'data' => ['id']
    ]
] );

// Trigger: ?item_updated=1&id=123
// Shows: "Item #123 updated."

Configuration Options

Notice Configuration

Option Type Description Default
message string|callable The notice message or callback Required
type string Notice type: success, error, warning, info success
trigger string GET parameter that triggers the notice Key name
data array GET parameters to pass to message callback []
condition callable Function to determine if notice should show null
persistent bool Shows on every page load when condition is met false
dismissible bool Whether notice can be dismissed true
capability string Required capability to see notice null
pages array|string Admin pages where notice appears ('all' for global) null

Global Options

register_admin_notices( 'myplugin', $notices, [
    'pages'       => ['myplugin', 'myplugin-*'],  // Wildcard support
    'capability'  => 'manage_options',
    'dismissible' => true                          // Default for all notices
] );

Type Detection

The system automatically infers notice types from key names:

register_admin_notices( 'myplugin', [
    'item_updated'        => 'Updated!',    // success (default)
    'item_deleted_error'  => 'Failed!',     // error (suffix)
    'warning_low_stock'   => 'Low stock',   // warning (prefix)
    'info_new_feature'    => 'New feature'  // info (prefix)
] );

Real-World Example

// In your plugin's main file or admin initialization
add_action( 'admin_init', function() {
    register_admin_notices( 'woocommerce_extras', [
        // Simple CRUD notifications
        'product_added'   => __( 'Product added successfully.', 'wc-extras' ),
        'product_updated' => __( 'Product updated successfully.', 'wc-extras' ),
        'product_deleted' => __( 'Product deleted successfully.', 'wc-extras' ),
        
        // Bulk operations with counts
        'bulk_updated' => [
            'message' => function( $args ) {
                $count = absint( $args['count'] ?? 0 );
                return sprintf( 
                    _n( 
                        '%d product updated.', 
                        '%d products updated.', 
                        $count, 
                        'wc-extras' 
                    ), 
                    $count 
                );
            },
            'data' => ['count']
        ],
        
        // Persistent warnings
        'shipping_not_configured' => [
            'condition' => function() {
                $zones = WC_Shipping_Zones::get_zones();
                return empty( $zones );
            },
            'message' => __( 'No shipping zones configured. Products cannot be shipped.', 'wc-extras' ),
            'type' => 'warning',
            'persistent' => true
        ],
        
        // Error handling
        'import_failed' => [
            'message' => function( $args ) {
                $error = $args['error'] ?? __( 'Unknown error', 'wc-extras' );
                $line = $args['line'] ?? 0;
                
                if ( $line > 0 ) {
                    return sprintf( 
                        __( 'Import failed at line %d: %s', 'wc-extras' ), 
                        $line, 
                        esc_html( $error ) 
                    );
                }
                
                return sprintf( __( 'Import failed: %s', 'wc-extras' ), esc_html( $error ) );
            },
            'type' => 'error',
            'data' => ['error', 'line']
        ]
    ], [
        'pages' => ['woocommerce', 'edit-product', 'product'],
        'capability' => 'manage_woocommerce'
    ] );
} );

// In your form handler
function handle_product_save() {
    $product_id = save_product( $_POST );
    
    if ( is_wp_error( $product_id ) ) {
        $url = add_query_arg( [
            'import_failed' => 1,
            'error' => $product_id->get_error_message()
        ], admin_url( 'edit.php?post_type=product' ) );
    } else {
        $url = add_query_arg( [
            'product_updated' => 1,
            'id' => $product_id
        ], admin_url( 'edit.php?post_type=product' ) );
    }
    
    wp_redirect( $url );
    exit;
}

Requirements

  • PHP 7.4 or later
  • WordPress 5.0 or later

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the GPL-2.0-or-later License.

Credits

Created by David Sherlock at ArrayPress.

arraypress/wp-register-notices 适用场景与选型建议

arraypress/wp-register-notices 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「notifications」 「wordpress」 「messages」 「error-handling」 「wp-admin」 「notices」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 arraypress/wp-register-notices 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 arraypress/wp-register-notices 我们能提供哪些服务?
定制开发 / 二次开发

基于 arraypress/wp-register-notices 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2025-01-10