如何在插件激活的管理面板中显示通知? [英] How to display notice in admin panel on Plugin Activation?

查看:87
本文介绍了如何在插件激活的管理面板中显示通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我激活测试插件时,我试图在管理面板中显示一个通知.
如何显示?那是什么方法?

I am trying to display a notice in admin panel when I activate my test plugin.
How can I display that? What's that method?

推荐答案

对于插件激活,由于存在重定向,因此无法直接使用'admin_notices'挂钩.一种解决方法是将您的通知存储在选项表中,并在下次进行检查.另外,如果您还想介绍插件升级和激活,则需要使用另一个钩子,例如'admin_init'(自WP 3.1起,请参见

For plugin activations, the 'admin_notices' hook cannot be used directly, because there is a redirect. A workaround is to store your notice in the options table and check for it next time. Also, if you also want to cover plugin upgrades as well as activations, you will need to use another hook, such as 'admin_init' (since WP 3.1, see http://make.wordpress.org/core/2010/10/27/plugin-activation-hooks/).

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

Here is a complete sample plugin handling both activation and upgrade. I made the deferred notice an array so you can stack them up.

<?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的几乎所有地方都相同:

UPDATE: There's also a common way to use set_transient() instead of update_option(), and to direct messages to the correct admin user. This post concerns metaboxes, not plugin activation, but the techniques work the same just about everywhere in Dashboard, as far as I know: https://wordpress.stackexchange.com/questions/15354/passing-error-warning-messages-from-a-meta-box-to-admin-notices

这篇关于如何在插件激活的管理面板中显示通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆