WordPress 主题开发之激活主题后显示自定义提示信息

如果你要开发一款WordPress 主题,那么你肯定希望用户在激活主题后能够看到你自定义的提示信息,一般是感谢使用主题之类的话。WordPress 本身在激活主题后会有一句“新主题已启用。查看站点”。我们想要的效果也是这样,不过提示的文字不同罢了。

将下面的代码丢入主题的functions.php 文件即可实现激活主题后显示自定义提示信息:

//激活主题后显示自定义提示信息 devework.com
add_action('admin_notices', 'admin_notice');
function 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, 'ignore_notice') ) {
        echo '<div class="updated"><p>'; 
        printf(__('感谢您选择Devework 主题!请前往<strong><a href="' . get_admin_url() . 'themes.php?page=xxxx' . '">主题设置页面</a></strong> 以更好地使用本主题。'), '?nag_ignore=0');
        echo "</p></div>";
	}
}
add_action('admin_init', 'nag_ignore');
function 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['nag_ignore']) && '0' == $_GET['nag_ignore'] ) {
             add_user_meta($user_id, 'ignore_notice', 'true', true);
	}
}

根据自己的需要修改相关代码,实现的效果如下图:

这段代码还有个好处,如果点击了该提示,那么下次不会再次显示。

代码来源,cssreflex,感谢原作者。

评分:
当前平均分 0.00 (0%) - 0 个投票
2 条 评论
  1. 点击之后并没有消失 请楼主修复一下哦

    9年前 回复
  2. 我是来观摩大侠的。

    10年前 回复
发表评论