主题开发文档

💡 云策文档标注

概述

WordPress 4.9.6 引入了多项工具以帮助网站符合欧盟 GDPR 等法规要求。本文档详细说明了主题作者在兼容这些功能时需了解的关键信息,主要涉及隐私政策页面和评论 Cookie 处理。

关键要点

  • WordPress 4.9.6 新增隐私功能,主题通常无需直接修改,但需确保设计或功能不冲突。
  • 隐私政策页面可通过后台设置选择,新安装站点会自动创建草稿页面。
  • 提供函数如 get_privacy_policy_url() 和 the_privacy_policy_link() 用于获取或输出隐私政策链接。
  • 可自定义隐私政策模板(如 privacy-policy.php 或 templates/privacy-policy.html),但需使用 the_content() 或 Post Content block 输出内容。
  • 评论表单新增 Cookie 存储复选框,默认未选中,用户需明确选择同意存储数据。
  • 复选框自动添加到 Comment Form block 或 comment_form() 函数中,主题作者应检查 CSS 是否需要调整。

代码示例

<?php the_privacy_policy_link( '<div>', '</div>' ); ?>

注意事项

  • 自定义隐私政策模板时,务必使用 the_content() 函数或 Post Content block 来正确显示用户内容。
  • 检查评论表单中的复选框及其标签,确保在自定义主题中 CSS 样式适配,避免布局问题。

📄 原文内容

WordPress 4.9.6 introduced several tools to help sites meet the requirements of the European Union’s new GDPR (General Data Protection Regulation) and other potential laws across the world. This article details what theme authors need to know about compatibility with the features.

For the most part, this should not directly impact themes, but you should make sure that nothing in your theme’s design or functionality conflicts with these features.

Privacy Policy page

WordPress lets users select a privacy policy page via the Settings > Privacy screen in the admin. For newly-created WordPress installs (since version 4.9.6), a privacy policy page is automatically created with a draft status.

Linking to the Privacy Policy page

If you need to manually link to the Privacy Policy page on a site, WordPress provides a few functions for getting or outputting the URL or link:

The following example will display the privacy policy link surrounded by a <div> element:

<?php the_privacy_policy_link( '<div>', '</div>' ); ?>

Privacy Policy page template

While it is generally unnecessary for most themes, you can add a custom Privacy Policy template to customize the output of the page. This is an optional template:

  • Block Themes: templates/privacy-policy.html
  • Classic Themes: privacy-policy.php

If you decide to create a custom template, make sure that you output the page’s content using the appropriate block or function:

This ensures that the user’s Privacy Policy content will be correctly displayed on the front end of the site.

Comment cookies

When a logged out user comments on a post, they are asked for their name, email, and website. This information is stored locally in the commenter’s browser for two purposes:

  1. When they leave another comment on the site, their name, email, and website will be pre-populated into the respective fields.
  2. If their comment is held for moderation, they can return to that post and remove the comment before it is approved.

The information stored in this cookie is for convenience and is not essential. Therefore, the user needs to be given the choice to opt in or opt out of the storage of this data. For this reason, WordPress outputs a checkbox in the comment form that allows commenters to opt-in to storing this data in the cookie. This checkbox will be unchecked by default, as opt-in is an action the user must explicitly approve.

The new checkbox field is automatically added to comment forms displayed using the Comment Form block (block themes) or the comment_form() function (classic themes).

While most themes will not require any action, it is recommended that you double check that the input and label does not require CSS adjustments in custom themes.