Tho*_*son 14 wordpress plugins activation admin panel
我在激活测试插件时试图在管理面板中显示通知.
我该如何显示?那是什么方法?
kit*_*hin 25
对于插件激活,'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');
}
Run Code Online (Sandbox Code Playgroud)
更新:还有一种常用的方法来set_transient()代替update_option()和将消息定向到正确的管理员用户.这篇文章涉及元变量,而不是插件激活,但据我所知,这些技术在Dashboard中的所有地方都是一样的:https://wordpress.stackexchange.com/questions/15354/passing-error-warning-messages-from -a-元盒到管理通知单
小智 7
这很简单,可以显示通知
function your_admin_notice(){
echo '<div class="updated">
<p>I am a little yellow notice.</p>
</div>';
}
add_action('admin_notices', 'your_admin_notice');
Run Code Online (Sandbox Code Playgroud)
但是,如果您想要显示可接受的通知,请尝试以下
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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想在某些页面上显示该通知,请尝试以下条件.
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');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20083 次 |
| 最近记录: |