wordpress 自定义_如何在WordPress中添加自定义管理员通知

wordpress 自定义

Do you want to add admin notices in WordPress? Admin notices are used by WordPress core, themes, and plugins to display warnings, notices, and important on screen information to users. In this article, we will show you how you can add admin notices in WordPress.

您是否要在WordPress中添加管理员通知? WordPress核心,主题和插件使用管理通知来向用户显示警告,通知和重要的屏幕信息。 在本文中,我们将向您展示如何在WordPress中添加管理员通知。

How to Add Admin Notices in WordPress
为什么以及何时在WordPress中使用管理员声明? (Why and When to Use Admin Notices in WordPress?)

WordPress uses admin notices to alert users about errors, warnings, and success messages.

WordPress使用管理员通知来警告用户有关错误,警告和成功消息。

Example of a default WordPress admin notice

Individual site owners, plugin authors, and theme developers can also use admin notices.

各个网站所有者,插件作者和主题开发人员也可以使用管理员声明。

If you are working on a website for clients who are not familiar with WordPress, then you can add admin notices to display helpful information across their WordPress admin area.

如果您正在为不熟悉WordPress的客户使用网站,则可以添加管理员通知,以在其WordPress 管理员区域显示有用的信息。

Custom admin notices can also be helpful if you run a multi-author WordPress site. You can add notices to guide new authors and help them find their way around.

如果您运行多作者 WordPress网站,则自定义管理员通知也可能会有所帮助。 您可以添加公告以指导新作者并帮助他们找到解决之道。

However, we recommend using admin notices carefully. They can be really annoying and may ruin the WordPress experience for your users.

但是,我们建议您谨慎使用管理员声明。 他们可能真的很烦人,并且可能会破坏您的用户的WordPress体验。

Having said that, let’s take a look at how you can add your own custom admin notices in WordPress.

话虽如此,让我们来看看如何在WordPress中添加自己的自定义管理员通知。

方法1:在WordPress中手动添加自定义通知 (Method 1: Add Custom Notices in WordPress Manually)

This method requires you to add code to your WordPress site. If you have never added code before, then take a look at our guide on pasting snippets from the web into WordPress.

此方法要求您将代码添加到WordPress网站。 如果您以前从未添加过代码,请查看我们的有关将Web片段粘贴到WordPress中的指南

Let’s get started.

让我们开始吧。

First you need to add this code to your theme’s functions.php file or a site-specific plugin.

首先,您需要将此代码添加到主题的functions.php文件或特定站点的插件中


function general_admin_notice(){
    global $pagenow;
    if ( $pagenow == 'options-general.php' ) {
         echo '<div class="notice notice-warning is-dismissible">
             <p>This notice appears on the settings page.</p>
         </div>';
    }
}
add_action('admin_notices', 'general_admin_notice');

This code displays a notice on the settings page with a yellow border and a button to close the notice. This is how it will appear on your site:

该代码在设置页面上显示一个带有黄色边框的通知和一个关闭通知的按钮。 它是如何显示在您的网站上的:

Custom admin notice example

If you study the code, you will notice that we have used $pagenow variable to detect the current page.

如果您研究代码,您会注意到我们已经使用$pagenow变量来检测当前页面。

After that we added the condition that checks if the current page meets the page where we want to display the notice.

之后,我们添加了检查当前页面是否符合我们要显示通知的页面的条件。

If it does, then we show the notice wrapped in a <div> element. This div element uses CSS classes already defined in the WordPress admin stylesheet for different type of notices.

如果是这样,那么我们将显示包装在<div>元素中的通知。 此div元素使用WordPress管理员样式表中已定义的CSS类来获取不同类型的通知。

You need to use notice class and then you can add notice-error, notice-warning, notice-success, or notice-info.

您需要使用notice类,然后可以添加notice-errornotice-warningnotice-successnotice-info

Optionally, you can use is-dismissible class which adds a button to close the notice.

(可选)您可以使用is-dismissible类,该类添加一个按钮以关闭通知。

Apart from checking the current page, you can add all kind of conditions to show notices matching different scenarios.

除了检查当前页面之外,您还可以添加各种条件以显示与不同方案匹配的通知。

For example, you want to display a notice only to users with the author user role.

例如,您只想向具有作者用户角色的用户显示通知。

Here is how you will do that:

这是您将执行的操作:


function author_admin_notice(){
    global $pagenow;
    if ( $pagenow == 'index.php' ) {
    $user = wp_get_current_user();
    if ( in_array( 'author', (array) $user->roles ) ) {
    echo '<div class="notice notice-info is-dismissible">
          <p>Click on <a href="edit.php">Posts</a> to start writing.</p>
         </div>';
    }
}
}
add_action('admin_notices', 'author_admin_notice');

As you can see that we have added an extra check to detect the user role in our function.

如您所见,我们添加了额外的检查以检测功能中的用户角色。

This is how it will appear on your site.

这就是它出现在您网站上的方式。

Custom notice by user role

Feel free to practice with different conditions, filters, and hooks to play with admin notices.

随意练习不同的条件,过滤器和挂钩,以播放管理员通知。

方法2:使用WordPress插件添加管理员通知 (Method 2: Add Admin Notices Using a WordPress Plugin)

This method is simpler as it does not require you to add code. However, it is not as flexible as the custom code method.

此方法比较简单,因为它不需要您添加代码。 但是,它不像自定义代码方法那样灵活。

First thing you need to do is install and activate the KJM Admin Notices plugin. For more details, see our step by step guide on how to install a WordPress plugin.

您需要做的第一件事是安装并激活KJM管理员声明插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。

Upon activation, you need to visit Settings » KJM Admin Notices page to configure plugin settings.

激活后,您需要访问设置»KJM管理员声明页面来配置插件设置。

KJM Admin Notices settings

First, you need to check the option to enable KJM Admin Notices. The second option adds a custom post type where you can add and edit your custom admin notices.

首先,您需要选中启用KJM管理员通知的选项。 第二个选项添加自定义帖子类型,您可以在其中添加和编辑自定义管理员通知。

The plugin also allows you to send an email to registered users when you publish a new notice. You can check the box next to ‘Send Email’ option if you want to use this feature.

该插件还允许您在发布新通知时向注册用户发送电子邮件 。 如果要使用此功能,可以选中“发送电子邮件”选项旁边的框。

You can also enable comments for your notices which will allow users to respond to notices by adding comments. To enable this feature, check the box next to ‘Allow Comments’ option.

您还可以为通知启用评论,这将允许用户通过添加评论来响应通知。 要启用此功能,请选中“允许评论”选项旁边的框。

Don’t forget to click on the save changes button to store your settings.

不要忘记单击“保存更改”按钮来存储您的设置。

You will now see a new menu item labeled notices in your WordPress admin bar. This is where you can add and edit your custom admin notices.

现在,您将在WordPress管理栏中看到一个新菜单项,其标签为“注意”。 您可以在此处添加和编辑自定义管理员通知。

Let’s create your first admin notice.

让我们创建您的第一个管理员通知。

Visit Notices » Add Notice page. You will see a screen much like the WordPress post edit screen.

访问公告»添加公告页面。 您将看到一个类似于WordPress帖子编辑屏幕的屏幕。

Add new custom notice

Start by adding a title for your notice, then add the actual notice in the post editor. You can select the notice category from the box on your right hand.

首先为您的通知添加标题,然后在帖子编辑器中添加实际通知。 您可以从右侧的框中选择通知类别。

Next you need to select the user roles which will see this notice.

接下来,您需要选择将看到此通知的用户角色。

Select user roles that will see the notice

You can optionally show or hide title, author and date, and the button to dismiss notice.

您可以选择显示或隐藏标题,作者和日期以及用于取消通知的按钮。

Once you are finished, click on the publish button and your custom admin notice will go live.

完成后,单击“发布”按钮,您的自定义管理员通知将生效。

KJM admin notices

KJM Admin Notices allows you to manage your custom admin notices without writing any code. You can delete or unpublish notices that you don’t want to display any more.

使用KJM管理员声明,您无需编写任何代码即可管理自定义管理员声明。 您可以删除或取消发布不想再显示的通知。

Using the email feature, you can also use it to alert all your users even if they don’t log in to check notices.

使用电子邮件功能,即使用户未登录检查通知,也可以使用它来提醒所有用户。

Having problems sending emails? See our guide on how to fix WordPress not sending email issue.

在发送电子邮件时遇到问题? 请参阅我们的指南,了解如何解决WordPress不发送电子邮件的问题

You may also want to take a look at WP Notification Center plugin. It adds a Facebook-like notification center in WordPress. Users can click on the notification icon to see their notifications.

您可能还想看看WP Notification Center插件。 它在WordPress中添加了类似Facebook的通知中心。 用户可以单击通知图标以查看其通知。

Notification center

That’s all. We hope this article helped you learn how to add custom admin notices in WordPress. You may also want to see our guide on how to create a custom user registration form in WordPress.

就这样。 我们希望本文能帮助您学习如何在WordPress中添加自定义管理员通知。 您可能还希望查看有关如何在WordPress中创建自定义用户注册表单的指南

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-admin-notices-in-wordpress/

wordpress 自定义

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值