WordPress页面及文章添加勾选框插入指定Meta代码的技术问询
Hey Shaun, let's break this down step by step to get that checkbox working exactly how you need it, without relying on third-party password protection plugins.
一、用Advanced Custom Fields (ACF) 实现勾选框(推荐)
1. 创建ACF字段组
First, head to your WordPress admin's ACF "Field Groups" page and create a new group:
- Field Group Name: Something like "Premium Content Settings"
- Field Type: Pick "Toggle" (it's more intuitive for a simple on/off checkbox) or "Checkbox" if you prefer
- Field Name: Use
is_premium_content(this is the internal slug you'll reference in code—don't forget it) - Field Label: "Mark as premium content"
- Location Rules: Set it to "Show if Post Type is equal to Post" AND "Show if Post Type is equal to Page" (so the field appears on both post and page edit screens)
- Save the field group, and you'll see the toggle/checkbox on every post and page edit page.
2. Add Code to Output the Meta Tag
Next, we'll use WordPress's wp_head hook to check if the field is checked, then inject the meta tag if needed. Add this code to your theme's functions.php file, or better yet, a custom functionality plugin (to avoid losing code when you update your theme):
add_action('wp_head', 'add_premium_content_meta_tag'); function add_premium_content_meta_tag() { // Only run on single post/page pages (not archive/list pages) if (is_singular(['post', 'page'])) { // Get the ACF field value $is_premium = get_field('is_premium_content'); // If the toggle is on (or checkbox is checked), output the meta tag if ($is_premium) { echo '<meta name="meetandengage-premium-content" content="true">' . "\n"; } } }
Note: If you used a Checkbox instead of a Toggle, ACF returns an array when checked. Adjust the condition to
!empty($is_premium)instead of just$is_premium.
二、No ACF? Use WordPress Native Custom Fields
If you don't want to use ACF, you can rely on WordPress's built-in custom fields:
1. Enable Custom Fields
On the post/page edit screen, click the three-dot menu in the top right, go to "Preferences", and check the "Custom Fields" box. You'll now see a custom fields section at the bottom of the edit page.
2. Add the Custom Field
For any post/page you want to mark as premium:
- Key:
is_premium_content - Value:
true
3. Update the Output Code
Swap out the ACF code with this native custom fields version:
add_action('wp_head', 'add_premium_content_meta_tag'); function add_premium_content_meta_tag() { if (is_singular(['post', 'page'])) { $is_premium = get_post_meta(get_the_ID(), 'is_premium_content', true); if ($is_premium === 'true') { echo '<meta name="meetandengage-premium-content" content="true">' . "\n"; } } }
三、About Post/Page List Pages (Archive Pages)
The official WordPress term for these list pages is Archive Pages. This includes your main blog post list (is_home()), category/tag pages, author pages, and any custom post type archives.
Your current need is to mark individual posts/pages for password protection, so the code above is set to only run on single post/page screens (is_singular()), which is exactly what you want—archive pages are collections of content, and your team will likely only need to protect individual entry pages.
四、Test It Out
Edit a post or page, toggle/check the "Mark as premium content" field, update the entry, then view the front-end page. Right-click > "View Page Source" and look for the <meta name="meetandengage-premium-content" content="true"> tag in the <head> section. If you uncheck the field, the tag should disappear from the source.
Your ops team can now scan for this meta tag via your server platform to apply password protection to those pages.
内容的提问来源于stack exchange,提问作者Shaun Taylor

