
子比主题优化:VIP 免费下载文章仅回复后可获取下载地址
在使用子比主题搭建资源站时,很多站长会遇到这样的需求:VIP 免费下载类文章,原本 VIP 会员可直接查看下载地址,但为了提升网站会员的互动活跃度,又不想同时设置 “会员下载” 和 “回复下载”(避免双重限制影响体验)。
针对这一痛点,下面分享两段实用代码,可实现VIP 免费文章仅需用户回复后即可查看下载地址的效果,核心优势如下:
✅ 无需修改子比主题核心代码,避免更新主题后失效;
✅ 直接将代码添加到主题的 functions.php 文件即可生效;
✅ 提供两种方案:全局设置(全站 VIP 免费文章生效)、指定文章设置(仅针对特定文章生效)。
版本一:全局生效版(所有VIP免费文章都强制回复)
[hidecontent type="reply"]
/**
* 【全局版】子比主题定制:VIP免费资源强制评论后才能下载
*/
add_filter('zibpay_posts_paid_box', 'zibll_vip_free_global_must_comment', 10, 3);
function zibll_vip_free_global_must_comment($html, $pay_mate, $post_id) {
// 1. 获取当前用户
$user_id = get_current_user_id();
if (!$user_id) return $html;
// 2. 获取权限状态
$paid = zibpay_is_paid($post_id, $user_id);
// 3. 核心判断:必须是 VIP且免费 类型 (排除单独购买的用户)
if ($paid && isset($paid['paid_type']) && strpos($paid['paid_type'], 'vip') !== false && strpos($paid['paid_type'], 'free') !== false) {
// 4. 检查是否已评论 (状态为 approve)
$args = array(
'user_id' => $user_id,
'post_id' => $post_id,
'count' => true,
'status' => 'approve'
);
$comment_count = get_comments($args);
// 5. 拦截:未评论则显示美化后的提示框
if ($comment_count == 0) {
$icon_url = ZIB_TEMPLATE_DIRECTORY_URI . '/img/null-2.svg'; // 修复:使用存在的图片
// 美化样式:VIP淡金背景 + 虚线边框
$box_style = 'background: linear-gradient(135deg, #fffdf5 0%, #fff 100%); border: 2px dashed #e8dcb6; border-radius: 8px;';
$new_html = '<div class="zib-widget pay-box" id="posts-pay" style="'.$box_style.'">';
$new_html .= '<div class="text-center padding-20 box-body">';
// 图片
$new_html .= '<div class="mb20"><img style="width:160px; opacity:0.8;" src="' . $icon_url . '"></div>';
// 标题和文案
$new_html .= '<div class="em12 mb10 c-yellow font-bold"><i class="fa fa-diamond mr6"></i>VIP 专享资源</div>';
$new_html .= '<div class="muted-2-color mb20" style="font-size:13px;">您拥有免费下载权限<br>为了社区活跃,请 <b>评论本文</b> 后刷新页面获取!</div>';
// 按钮
$new_html .= '<a href="#respond" onclick="if(document.getElementById(\'respond\')){document.getElementById(\'respond\').scrollIntoView({behavior:\'smooth\'});return false;}else{window.location.href=\'#respond\';}" class="but jb-yellow padding-lg radius"><i class="fa fa-commenting-o mr6"></i>前往评论</a>';
$new_html .= '</div></div>';
return $new_html;
}
}
return $html;
}
[/hidecontent]版本二:特定文章版(仅指定 ID 的文章生效)
[hidecontent type="reply"]
/**
* 【指定文章版】子比主题定制:指定文章VIP免费强制评论
*/
add_filter('zibpay_posts_paid_box', 'zibll_vip_free_specific_must_comment', 10, 3);
function zibll_vip_free_specific_must_comment($html, $pay_mate, $post_id) {
// =======================================================
// 🟢 配置区域:在这里填写文章ID,逗号隔开
// =======================================================
$target_ids = array(1234, 5678, 8888);
// 1. 如果不是指定文章,直接忽略
if (!in_array($post_id, $target_ids)) {
return $html;
}
// 2. 获取当前用户
$user_id = get_current_user_id();
if (!$user_id) return $html;
// 3. 获取权限状态
$paid = zibpay_is_paid($post_id, $user_id);
// 4. 核心判断:必须是 VIP且免费 类型
if ($paid && isset($paid['paid_type']) && strpos($paid['paid_type'], 'vip') !== false && strpos($paid['paid_type'], 'free') !== false) {
// 5. 检查是否已评论
$args = array(
'user_id' => $user_id,
'post_id' => $post_id,
'count' => true,
'status' => 'approve'
);
$comment_count = get_comments($args);
// 6. 拦截:未评论则显示美化后的提示框
if ($comment_count == 0) {
$icon_url = ZIB_TEMPLATE_DIRECTORY_URI . '/img/null-2.svg'; // 修复图片路径
// 美化样式:VIP淡金背景 + 虚线边框
$box_style = 'background: linear-gradient(135deg, #fffdf5 0%, #fff 100%); border: 2px dashed #e8dcb6; border-radius: 8px;';
$new_html = '<div class="zib-widget pay-box" id="posts-pay" style="'.$box_style.'">';
$new_html .= '<div class="text-center padding-20 box-body">';
// 图片
$new_html .= '<div class="mb20"><img style="width:160px; opacity:0.8;" src="' . $icon_url . '"></div>';
// 标题和文案
$new_html .= '<div class="em12 mb10 c-yellow font-bold"><i class="fa fa-diamond mr6"></i>VIP 专享资源</div>';
$new_html .= '<div class="muted-2-color mb20" style="font-size:13px;">您拥有免费下载权限<br>为了社区活跃,请 <b>评论本文</b> 后刷新页面获取!</div>';
// 按钮
$new_html .= '<a href="#respond" onclick="if(document.getElementById(\'respond\')){document.getElementById(\'respond\').scrollIntoView({behavior:\'smooth\'});return false;}else{window.location.href=\'#respond\';}" class="but jb-yellow padding-lg radius"><i class="fa fa-commenting-o mr6"></i>前往评论</a>';
$new_html .= '</div></div>';
return $new_html;
}
}
return $html;
}
[/hidecontent]

