wordpress 如何在插件激活的管理面板中显示通知?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9807064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display notice in admin panel on Plugin Activation?
提问by Thompson
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?
当我激活我的测试插件时,我试图在管理面板中显示一条通知。
我怎样才能显示它?那是什么方法?
回答by kitchin
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/).
对于插件激活,不能直接使用 'admin_notices' 钩子,因为有重定向。解决方法是将您的通知存储在选项表中,并在下次检查。此外,如果您还想涵盖插件升级和激活,则需要使用另一个钩子,例如“admin_init”(自 WP 3.1 起,请参阅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');
}
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
更新:还有一种常用方法可以使用set_transient()
代替update_option()
, 并将消息定向到正确的管理员用户。这篇文章涉及元框,而不是插件激活,但据我所知,这些技术在仪表板中几乎无处不在:https: //wordpress.stackexchange.com/questions/15354/passing-error-warning-messages-from -a-meta-box-to-admin-notices
回答by Ibnul Hasan
This is so simple to show a notice
显示通知如此简单
function your_admin_notice(){
echo '<div class="updated">
<p>I am a little yellow notice.</p>
</div>';
}
add_action('admin_notices', 'your_admin_notice');
But if you want to show a Dismissible Notice then try below
但是,如果您想显示可取消通知,请尝试以下操作
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);
}
}
And if you want to show that notice on certain page try below condition.
如果您想在特定页面上显示该通知,请尝试以下条件。
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');
回答by ronakg
Just use a <div class='updated'>
. For example -
只需使用一个<div class='updated'>
. 例如 -
echo "<div class='updated'>Test Plugin Notice</div>";
回答by Wyck
You can use the new admin notices to create what are called admin pointers using show_wp_pointer_admin_bar
.
您可以使用新的管理通知来创建所谓的管理指针show_wp_pointer_admin_bar
。
Linky: http://wpengineer.com/2272/how-to-add-and-deactivate-the-new-feature-pointer-in-wordpress-3-3/
链接:http://wpengineer.com/2272/how-to-add-and-deactivate-the-new-feature-pointer-in-wordpress-3-3/
回答by Eugene Manuilov
The proper way to add your notices is to echo it in your hook for admin_notices
action:
添加通知的正确方法是在您的admin_notices
操作挂钩中回显它:
function wpse8170_admin_notice(){
echo '<div class="updated"><p>This is my notice.</p></div>';
}
add_action('admin_notices', 'wpse8170_admin_notice');
回答by Yoav Kadosh
I've developed amarkal-admin-notification- a script that lets you add static/dismissible admin notices and handles the dismissal for you. This script is a module within the Amarkal WordPress framework.
我开发了amarkal-admin-notification- 一个脚本,可让您添加静态/可关闭的管理员通知并为您处理解雇。该脚本是 Amarkal WordPress 框架中的一个模块。
For example:
例如:
amarkal_admin_notification( 'my-error-notice', __('Oh snap! This is an <strong>error</strong> message.','slug'), 'error');