为多作者博客构建免责声明公告插件

亦凉 2022-12-03 11:24 1323阅读 0赞

A friend of mine operates a multi-author blog powered by WordPress.

我的一个朋友经营着一个由WordPress驱动的多作者博客。

To prevent any legal trouble, he often adds a ‘Disclaimer’ in every post made by guest authors which he did by editing and including the disclaimer text before publication.

为了防止任何法律麻烦,他经常在来宾作者的每篇帖子中添加“免责声明”,他在发布前通过编辑并包括免责声明文本来做到这一点。

I’m sure you will agree, that editing and adding disclaimers in every post made by guest authors seems a daunting task.

我相信您会同意,在来宾作者的每篇文章中编辑和添加免责声明似乎是一项艰巨的任务。

In this article, we will build a simple Disclaimer Notice plugin that will have an option page where a site administrator can add the disclaimer text which automatically gets appended immediately before or after the post content.

在本文中,我们将构建一个简单的“免责声明通知”插件,该插件将具有一个选项页,站点管理员可以在其中添加disclaimer text ,该disclaimer text会在帖子内容之前或之后立即自动添加。

插件开发 (Plugin Development)

To begin the plugin development, we need to include the plugin header in the plugin PHP file.

要开始插件开发,我们需要在插件PHP文件中包含插件头。

Without the header, WordPress will not recognize the plugin.

没有标题,WordPress将无法识别该插件。

  1. <?php
  2. /*
  3. Plugin Name: Disclaimer Manager
  4. Plugin URI: https://www.sitepoint.com
  5. Description: Easy Disclaimer Manager for Multi-author blogs.
  6. Version: 1.0
  7. Author: Agbonghama Collins
  8. Author URI: http://w3guy.com
  9. License: GPL2
  10. */

To get started building the settings page for the plugin; first, we will add the sub menu page to the ‘Settings’ menu using the function add_options_page placed in a function registered with the admin_menu.

开始构建插件的设置页面; 首先,我们将使用在admin_menu注册的函数中放置的add_options_page函数将子菜单页面添加到“设置”菜单。

  1. // Add the admin options page
  2. add_action( 'admin_menu', 'dm_settings_page' );
  3. function dm_settings_page() {
  4. add_options_page( 'Disclaimer Manager', 'Disclaimer Manager', 'manage_options', 'disclaimer-manager', 'dm_options_page' );
  5. }

The argument passed to add_options_page() are as follows:

传递给add_options_page()的参数如下:

  • Disclaimer Manager: The text to be displayed in the title tags of the page when the menu is selected.

    Disclaimer Manager :选择菜单时在页面标题标签中显示的文本。

  • Disclaimer Manager: The text to be used for the menu.

    Disclaimer Manager :用于菜单的文本。

  • manage_options: The capability required for this menu to be displayed to the user.

    manage_options :向用户显示此菜单所需的功能 。

  • disclaimer-manager: The slug name to refer to this menu.

    disclaimer-manager :引用此菜单的子名称 。

  • dm_options_page: The function to be called to output the plugin settings page.

    dm_options_page :要输出插件设置页面的函数。

Below, is the code for the callback function dm_options_page that will display the settings page.

下面是用于显示设置页面的回调函数dm_options_page的代码。

  1. // Draw the options page
  2. function dm_options_page() {
  3. ?>
  4. <div class="wrap">
  5. <?php screen_icon(); ?>
  6. <h2> Disclaimer Manager for Authors </h2>
  7. <form action="options.php" method="post">
  8. <?php settings_fields( 'disclaimer_manager_options' ); ?>
  9. <?php do_settings_sections( 'disclaimer-manager' ); ?>
  10. <?php submit_button(); ?>
  11. </form>
  12. </div>
  13. <?php
  14. }

The WordPress Settings API is being used to build and manage the settings form.

WordPress 设置API用于构建和管理设置表单。

The settings_fields function in dm_options_page() above output the nonce, action, and form fields for the settings page while the do_settings_sections() prints out all settings sections added to a particular settings page.

上面dm_options_page()的settings_fields函数输出设置页面的现时,动作和表单字段,而do_settings_sections()打印出添加到特定设置页面的所有设置部分。

Below is the full Settings API code for the settings page.

以下是设置页面的完整设置API代码。

  1. // Register and define the settings
  2. add_action( 'admin_init', 'dm_admin_init' );
  3. function dm_admin_init() {
  4. register_setting( 'disclaimer_manager_options', 'disclaimer_manager_options',
  5. '' );
  6. add_settings_section( 'dm_main', 'Plugin Settings',
  7. '', 'disclaimer-manager' );
  8. add_settings_field( 'dm_textarea-id', 'Enter Disclaimer Text',
  9. 'disclaimer_text_textarea', 'disclaimer-manager', 'dm_main' );
  10. add_settings_field( 'dm_select-id', 'Disclaimer Position',
  11. 'disclaimer_text_position', 'disclaimer-manager', 'dm_main' );
  12. }
  13. // Display and fill the form field
  14. function disclaimer_text_textarea() {
  15. // get option 'disclaimer_text' value from the database
  16. $options = get_option( 'disclaimer_manager_options' );
  17. $disclaimer_text = $options['disclaimer_text'];
  18. // echo the field
  19. echo "<textarea rows='8' cols='50' id='disclaimer_text' name='disclaimer_manager_options[disclaimer_text]' >$disclaimer_text</textarea>";
  20. }
  21. function disclaimer_text_position() {
  22. // get option 'disclaimer_position' value from the database
  23. $options = get_option( 'disclaimer_manager_options' );
  24. $disclaimer_position = $options['disclaimer_position'];
  25. echo '<select name="disclaimer_manager_options[disclaimer_position]">';
  26. echo '<option value="top"' . selected( $disclaimer_position, 'top' ) . '>Top</option>';
  27. echo '<option value="bottom"' . selected( $disclaimer_position, 'bottom' ) . '>Bottom</option>';
  28. echo '</select>';
  29. }

Take Note: The register setting() registers the setting.

注意: register setting()注册设置。

The add_settings_section() creates settings sections – groups of settings you see on WordPress settings pages with a shared heading.

add_settings_section()创建设置部分 -您在WordPress设置页面上看到的带有共享标题的设置组。

The add_settings_field() registers a settings field to a settings page and section.

add_settings_field()将设置字段注册到设置页面和部分。

The get_option() retrieves the values of the settings form from the database and the update_option() saves the form values to the database.

get_option()从数据库中检索设置表单的值,而update_option()将表单值保存到数据库。

We are done building the settings page for the plugin.

我们已经完成了插件的设置页面的构建。

Below is a screenshot of the plugin settings page.

以下是插件设置页面的屏幕截图。

Disclaimer Notice Plugin - Image 1

The function add_disclaimer_to_post as its name implies, appends the ‘Disclaimer’ text to the top or bottom of every post as defined in the plugin settings page.

顾名思义,功能add_disclaimer_to_post将“免责声明”文本附加到插件设置页面中定义的每个帖子的顶部或底部。

  1. function add_disclaimer_to_post( $content ) {
  2. $options = get_option( 'disclaimer_manager_options' );
  3. // get disclaimer text form DB
  4. $disclaimer_text = $options['disclaimer_text'];
  5. // get disclaimer position from DB
  6. $disclaimer_position = $options['disclaimer_position'];
  7. // ensure we are in a post and not a page
  8. if ( is_single() ) : // if disclaimer position is set to top
  9. {
  10. if ( $disclaimer_position == 'top' ) {
  11. $content = $disclaimer_text . $content;
  12. }
  13. // if disclaimer position is set to bottom
  14. if ( $disclaimer_position == 'bottom' ) {
  15. $content .= $disclaimer_text;
  16. }
  17. }
  18. endif;
  19. return $content;
  20. }

Allow me to explain what the code above does.

请允许我解释以上代码的作用。

The ‘Disclaimer Text’ and its position are retrieved from the database and saved to the variables $disclaimer_text and $disclaimer_position.

从数据库中检索“免责声明文本”及其位置,并将其保存到变量$disclaimer_text$disclaimer_position

Next, we use the Boolean WordPress function is_single() to ensure we are dealing with a post and not an attachment or page.

接下来,我们使用布尔值WordPress函数is_single()来确保我们处理的是帖子而不是附件或页面。

The next two if conditional statements append the disclaimer to the top or bottom of the post content depending on the outcome of $disclaimer_position.

接下来的两个if条件语句取决于$disclaimer_position的结果,将免责声明附加到帖子内容的顶部或底部。

To put the function to work, we need to hook it to the content filter (used to filter the content of a post after it is retrieved from the database and before it is printed to the screen).

为了使该功能正常工作,我们需要将其挂接到the content过滤器(用于从数据库中检索帖子的内容之后,再将其打印到屏幕上,以过滤该帖子的内容)。

  1. add_filter( 'the_content', 'add_disclaimer_to_post' );

Voila! We are done coding the ‘Disclaimer Plugin’.

瞧! 我们已经完成了“免责声明插件”的编码。

Here is a screenshot of the plugin in action:

这是运行中的插件的屏幕截图:

Disclaimer Notice Plugin Image 2

结论 (Conclusion)

To further understand how the plugin was built and how you can implement it in your WordPress site, download the plugin.

要进一步了解插件的构建方式以及如何在WordPress网站中实现该插件 ,请下载 。

If you are looking for an advanced disclaimer plugin with features such as:

如果您正在寻找具有以下功能的高级免责声明插件:

  • Ability to choose the authors that will have a disclaimer or notification displayed in their post.

    能够选择将在其帖子中显示免责声明或通知的作者。

  • Built-in editor for adding CSS Styles for the ‘Disclaimer’ text / notification and lots more.

    内置的编辑器,用于为“免责声明”文本/通知添加CSS样式等。

Grab the improved version from the WordPress Plugin Directory.

从WordPress插件目录中获取改进的版本。

Let me know your thoughts in the comments.

在评论中让我知道您的想法。

翻译自: https://www.sitepoint.com/building-a-disclaimer-notice-plugin/

发表评论

表情:
评论列表 (有 0 条评论,1323人围观)

还没有评论,来说两句吧...

相关阅读

    相关 迁移公告

    叮~ 为了更好的阅读体验和更快的打开速度,博主将陆续把博客从博客园迁移到[个人博客][Link 1]上 【博客园仍会同步保持更新】,不过想要获得最佳体验还请前往[个人博

    相关 声明

    本人为计算机学生一枚,CSDN博客作为本人记录解决方案的集合地,方便博主遇到同样的问题进行解决,并提供给网友一些思路。 但本人的学艺不精,并且写博客多为记录式,一些自己理解

    相关 搬运声明

    我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:[https://cloud.tencent.com/developer/support-plan?invite\_cod

    相关 免责声明

    声明: 本人并不接受任何无理由的嘲讽和谩骂。 首先我写博客并没有任何利益所得,如果对于我的博客有任何异议,麻烦请详细提出问题所在,我看到信息如果是正确的肯定会更正。我的初心

    相关 免责声明

    在法律允许的范围内,本网站在此声明,不承担用户或任何人士就使用或未能使用本网站所提供的信息或任何链接或项目所引致的任何直接、间接、附带、从属、特殊、惩罚性或惩戒性的损害赔...