如何在插件激活时在管理员面板中显示通知?

15

我正在尝试在管理面板激活我的测试插件时显示一个通知。
如何显示它?使用什么方法?

6个回答

29

对于插件激活,不能直接使用“admin_notices”钩子,因为存在重定向。一种解决方法是将提示存储在选项表中,并在下次检查时进行验证。此外,如果您还想覆盖插件升级以及激活,则需要使用另一个钩子,例如“admin_init”(自 WP 3.1 起,请参见http://make.wordpress.org/core/2010/10/27/plugin-activation-hooks/)。

这里是一个完整的示例插件,处理激活和升级。我将延迟提示设置为数组,以便您可以将它们堆叠起来。

<?php
/*
Plugin Name: My Plugin
*/

register_activation_hook(__FILE__, 'my_plugin_activation');
function my_plugin_activation() {
  $notices= get_option('my_plugin_deferred_admin_notices', array());
  $notices[]= "My Plugin: Custom Activation Message";
  update_option('my_plugin_deferred_admin_notices', $notices);
}

add_action('admin_init', 'my_plugin_admin_init');
function my_plugin_admin_init() {
  $current_version = 1;
  $version= get_option('my_plugin_version');
  if ($version != $current_version) {
    // Do whatever upgrades needed here.
    update_option('my_plugin_version', $current_version);
    $notices= get_option('my_plugin_deferred_admin_notices', array());
    $notices[]= "My Plugin: Upgraded version $version to $current_version.";
    update_option('my_plugin_deferred_admin_notices', $notices);
  }
}

add_action('admin_notices', 'my_plugin_admin_notices');
function my_plugin_admin_notices() {
  if ($notices= get_option('my_plugin_deferred_admin_notices')) {
    foreach ($notices as $notice) {
      echo "<div class='updated'><p>$notice</p></div>";
    }
    delete_option('my_plugin_deferred_admin_notices');
  }
}

register_deactivation_hook(__FILE__, 'my_plugin_deactivation');
function my_plugin_deactivation() {
  delete_option('my_plugin_version'); 
  delete_option('my_plugin_deferred_admin_notices'); 
}

更新:还有一种常见方法是使用set_transient()而不是update_option(),并将消息发送到正确的管理用户。这篇文章涉及元框而不是插件激活,但据我所知,这些技术在仪表盘的几乎所有地方都适用:https://wordpress.stackexchange.com/questions/15354/passing-error-warning-messages-from-a-meta-box-to-admin-notices


这看起来不错,我唯一的评论是让你在添加新通知的部分“DRY up”。 - pguardiario
是的,这三行代码$notices= get_option(...); $notices[]=...; update_option(..., $notices)可以抽象成通用的my_plugin_add_notice()函数。你经常会看到它带有“note”和“error”参数。然后渲染函数以WP风格显示它,作为蓝色或红色横幅,使用CSS类“update”或“error”,如果我没记错的话。 - kitchin
绝妙的解决方案。谢谢。 - Alaksandar Jesus Gene
这个方法是可行的,但你必须在每个管理员页面上运行 admin_notices。难道没有一种方法可以直接添加到 WordPress 的通知中吗?似乎每个插件都必须实现这种功能。 - Leo

8

这是一个非常简单的通知展示方式

function your_admin_notice(){
echo '<div class="updated">
   <p>I am a little yellow notice.</p>    
</div>';    
}
add_action('admin_notices', 'your_admin_notice');

但如果你想显示一个可关闭的通知,则可以尝试以下内容。
    add_action('admin_notices', 'example_admin_notice');

function example_admin_notice() {
    global $current_user ;
        $user_id = $current_user->ID;
        /* Check that the user hasn't already clicked to ignore the message */
    if ( ! get_user_meta($user_id, 'example_ignore_notice') ) {
        echo '<div class="updated"><p>'; 
        printf(__('This is an annoying nag message.  Why do people make these? | <a href="%1$s">Hide Notice</a>'), '?example_nag_ignore=0');
        echo "</p></div>";
    }
}

add_action('admin_init', 'example_nag_ignore');

function example_nag_ignore() {
    global $current_user;
        $user_id = $current_user->ID;
        /* If user clicks to ignore the notice, add that to their user meta */
        if ( isset($_GET['example_nag_ignore']) && '0' == $_GET['example_nag_ignore'] ) {
             add_user_meta($user_id, 'example_ignore_notice', 'true', true);
    }
}

如果您想在特定页面上显示该通知,请尝试下面的条件。

    function my_admin_notice(){
    global $pagenow;
    if ( $pagenow == 'plugins.php' ) {
         echo '<div class="updated">
             <p>This notice only appears on the plugins page.</p>
         </div>';
    }
}
add_action('admin_notices', 'my_admin_notice');

You can see here


我是否理解正确,我需要为每个要显示的消息创建一个回调函数?我该如何创建一个带有参数的函数,以指定错误消息是什么? - chizou
好的,如果您想要显示错误信息,则实际上有其他方法。 要显示带参数的admin_notice,您可以尝试此处最高票答案。另外,您也可以从以下链接中找到一种方法: https://dev59.com/mkjSa4cB1Zd3GeqPIcgB - Ibnul Hasan

3
只需使用 <div class='updated'>。例如 -
echo "<div class='updated'>Test Plugin Notice</div>";

是的,但是这个通知会一直显示,我希望当我点击该通知中的配置链接(激活后),该通知会消失。 - Thompson
1
在这种情况下,您只需要添加一个标志,用于存储用户是否访问了插件的配置。您可以将此标志存储在“wp_options”表中。 - ronakg

3

1

添加通知的正确方式是在admin_notices动作的挂钩中回显它:

function wpse8170_admin_notice(){
    echo '<div class="updated"><p>This is my notice.</p></div>';
}
add_action('admin_notices', 'wpse8170_admin_notice');

1
但是它不会像问题中描述的那样在插件激活时显示。 - Progrock

0

我开发了amarkal-admin-notification - 一个脚本,可以让您添加静态/可关闭的管理员通知,并为您处理关闭。该脚本是Amarkal WordPress框架中的一个模块。

例如:

amarkal_admin_notification( 'my-error-notice', __('Oh snap! This is an <strong>error</strong> message.','slug'), 'error');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接