如何为自定义文章类型添加“部分免费”模式并支持搜索引擎抓取?
解读
国内内容付费场景下,“部分免费”=首段或指定字数免费,剩余内容登录/付费后可见。面试官想验证三件事:
- 能否在不破坏 SEO 友好性 的前提下把免费片段交给搜索引擎;
- 能否用 原生 WordPress 机制(CPT、Meta、Hook、REST)实现,而非硬编码;
- 能否兼顾 性能、安全、缓存、国内 CDN 合规(如阿里云、腾讯云对“登录态”缓存的特殊策略)。
回答必须体现“让蜘蛛看到免费段,让人看到付费墙”的核心思路,并给出可落地的代码级方案。
知识点
- register_post_type 时开启
'public'=>true,'publicly_queryable'=>true保证蜘蛛可访问 - save_post 钩子拆分内容:免费段存
post_excerpt,完整段存post_content,或拆到wp_postmeta(meta_key=_ct_free_content) - template_redirect 或 the_content Filter 里做“蜘蛛识别 → 输出免费段,人 → 输出付费墙”
- 蜘蛛识别:优先用 HTTP_UA 正则匹配百度、搜狗、360、谷歌、必应;国内还需识别 神马 spider;同时用 is_login() 与 is_user_logged_in() 排除已登录用户
- 付费墙模板:用 wp_die() 返回 200 状态码(避免 403 被蜘蛛判死链),正文只输出免费段+“登录后阅读全文”纯文本,不输出购买按钮的 a 标签(防止蜘蛛爬付费链接)
- SEO 三件套:
– 免费段必须放在正文最前,保证<p>标签闭合,长度 80–120 中文字符;
– 主动推送到 百度普通收录 API(wp_remote_post),字段中只含免费段;
– 在 head 加<meta name="robots" content="max-snippet:-1, max-image-preview:large">允许蜘蛛抓取完整免费段 - 缓存:国内 CDN 普遍 忽略 Cookie,需在边缘节点用 Edge-Cookie-Vary 或 Nginx lua 区分“已登录”缓存切片;开发环境用 Batcache 或 Redis Object Cache 时,把
is_user_logged_in()作为缓存 key 的一部分 - 安全:免费段输出用 wp_kses_post 过滤,防止作者插 iframe 挂马;postmeta 字段加 $wpdb->prepare 防注入
- 国际化:用 __() 包裹“登录后阅读”,方便后续接入 Weglot 或 WPML 做出海
- 单元测试:WP_UnitTestCase 里伪造百度 UA,断言
get_the_content()只返回免费段;同时断言 status=200 且 strpos(‘登录后阅读’)===false(对蜘蛛不应出现中文提示)
答案
- 注册 CPT
add_action('init', function(){
register_post_type('novel',[
'public' => true,
'publicly_queryable'=> true,
'show_in_rest' => true,
'supports' => ['title','editor','excerpt'],
'has_archive' => true,
'rewrite' => ['slug'=>'book'],
]);
});
- 保存时拆段
add_action('save_post_novel', function($post_id){
if (defined('DOING_AUTOSAVE')) return;
$whole = wp_kses_post($_POST['content']);
$free = mb_substr($whole,0,120);
update_post_meta($post_id,'_ct_free_content',$free);
// 可选:把免费段同时写回 post_excerpt,方便 RSS
wp_update_post(['ID'=>$post_id,'post_excerpt'=>$free]);
},10,1);
- 内容过滤器
add_filter('the_content', function($content){
if (get_post_type()!=='novel') return $content;
// 蜘蛛 UA 池(国内常见)
$bots = '/(baiduspider|sogou|360spider|googlebot|bingbot|sm)/i';
$is_bot = isset($_SERVER['HTTP_USER_AGENT']) && preg_match($bots, $_SERVER['HTTP_USER_AGENT']);
$is_login = is_user_logged_in();
if ($is_bot || $is_login) return $content; // 蜘蛛或已登录:给全文
$free = get_post_meta(get_the_ID(),'_ct_free_content',true);
$html = $free . '<p>登录后阅读全文</p>';
// 返回 200,避免 403
status_header(200);
return $html;
}, 999);
- 主动推百度
add_action('save_post_novel', function($post_id){
$url = get_permalink($post_id);
$api = 'http://data.zz.baidu.com/urls?site=你的域名&token=你的token';
wp_remote_post($api, ['body'=>$url, 'headers'=>['Content-Type'=>'text/plain']]);
});
- Nginx 缓存切片(示例)
map $http_cookie $cache_uid {
default 0;
~*wordpress_logged_in 1;
}
fastcgi_cache_key "$scheme$request_method$host$request_uri$cache_uid";
保证 CDN 对“登录态”单独缓存,避免把付费墙内容误缓存给蜘蛛。
拓展思考
- 如果后续接入 微信小程序 阅读,需在 REST API 里新增
/wp-json/wp/v2/novel/{id}/free端点,返回纯文本免费段,供小程序 SEO 同步;同时用 jwt-auth 插件做登录态,防止抓包越权 - 对 极验、数美 这类国内人机验证,可在
template_redirect里先出验证码,通过后再种加密 Cookie,避免蜘蛛被挡 - 当站点使用 华为云 CDN 全站加速时,边缘函数 ES2 支持 UA 判断,可直接在边缘节点就返回免费段,回源 0 次,大幅降低源站压力
- 若内容含 Latex 数学公式,免费段截断需用 DOMDocument 按段落切,防止把未闭合的
[latex]短代码暴露给蜘蛛,导致搜索结果出现乱码 - 最后,面试时可反问面试官:
“贵司是否已接入 百度 MIP 或 谷歌 Web Stories?如果有,我可以把免费段同步输出成 AMP 格式,在移动端获得更好排名。”
体现你对国内搜索生态与国际化双轨并行的深度理解。