【问题标题】:How to display notice in admin panel on Plugin Activation?如何在插件激活的管理面板中显示通知?
【发布时间】:2012-04-06 02:47:14
【问题描述】:

当我激活我的测试插件时,我试图在管理面板中显示一条通知。
我怎样才能显示它?那是什么方法?

【问题讨论】:

    标签: wordpress plugins panel admin activation


    【解决方案1】:

    对于插件激活,不能直接使用“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(),并将消息定向到正确的管理员用户。这篇文章涉及元框,而不是插件激活,但据我所知,这些技术在 Dashboard 中几乎无处不在:https://wordpress.stackexchange.com/questions/15354/passing-error-warning-messages-from-a-meta-box-to-admin-notices

    【讨论】:

    • 这看起来不错,我唯一的意见是干掉添加新通知的部分。
    • 是的,$notices= get_option(...); $notices[]=...; update_option(..., $notices) 三行可以抽象为通用的my_plugin_add_notice() 函数。您经常会在“note”与“error”的参数中看到这一点。如果我没记错的话,然后渲染函数以 WP 方式将其显示为蓝色或红色横幅,使用 css 类“更新”或“错误”。
    • 极好的解决方案。谢谢
    【解决方案2】:

    显示通知就是这么简单

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

    但是,如果您想显示 Dismissible 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

    【讨论】:

    • 我是否正确地看到了我需要为每条要显示的消息创建一个回调函数?我将如何创建一个函数,该函数接受一个指定错误消息的参数?
    • 好吧,如果你想显示错误信息那么实际上还有其他方法。要显示带有参数的 admin_notice,您可以在此处尝试最重要的答案。您也可以从下面的链接中找到一种方法stackoverflow.com/questions/1242328/…
    【解决方案3】:

    只需使用&lt;div class='updated'&gt;。比如——

    echo "<div class='updated'>Test Plugin Notice</div>";
    

    【讨论】:

    • 是的,但是这个通知一直显示,我希望这个通知会消失,当我点击那个通知中的配置链接时(激活后)
    • 在这种情况下,您只需添加一个标志,如果用户访问了插件的配置,该标志将存储。您可以将此标志存储在wp_options 表中。
    【解决方案4】:

    您可以使用新的管理员通知通过show_wp_pointer_admin_bar 创建所谓的管理员指针。

    林奇:http://wpengineer.com/2272/how-to-add-and-deactivate-the-new-feature-pointer-in-wordpress-3-3/

    【讨论】:

      【解决方案5】:

      添加通知的正确方法是在admin_notices 操作的钩子中回显它:

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

      【讨论】:

      • 但插件激活时并没有显示为问题。
      【解决方案6】:

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

      例如:

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

      【讨论】:

        猜你喜欢
        • 2011-06-17
        • 2014-12-03
        • 1970-01-01
        • 2013-11-21
        • 1970-01-01
        • 1970-01-01
        • 2018-04-13
        • 2013-10-24
        • 1970-01-01
        相关资源
        最近更新 更多