discuz:纯代码实现隐藏内容VIP用户组直接可见

步骤 1:修改 source/function/function_discuzcode.php
function_discuzcode.php 是 Discuz! 处理 [hide] 标签的核心文件,找到处理 [hide] 的代码段(通常在 discuzcode 函数中)。

在文件中搜索 [hide] 代码,大概243行,找到以下代码:


			if(strpos($msglower, '[hide]') !== FALSE) {
				if($authorreplyexist === null) {
					if(!$_G['forum']['ismoderator']) {
						if($_G['uid']) {
							$_post = C::t('forum_post')->fetch_post('tid:'.$_G['tid'], $pid);
							$authorreplyexist = $_post['tid'] == $_G['tid'] ? C::t('forum_post')->fetch_pid_by_tid_authorid($_G['tid'], $_G['uid']) : FALSE;
						}
					} else {
						$authorreplyexist = TRUE;
					}
				}
				if($authorreplyexist) {
					$message = preg_replace("/\[hide\]\s*(.*?)\s*\[\/hide\]/is", tpl_hide_reply(), $message);
				} else {
					$message = preg_replace("/\[hide\](.*?)\[\/hide\]/is", tpl_hide_reply_hidden(), $message);
					$message = '<script type="text/javascript">replyreload += \',\' + '.$pid.';</script>'.$message;
				}
			}

替换成以下代码:

if (strpos($msglower, '[hide]') !== false) {
	global $_G;
	$usergroup = $_G['groupid'];
	$allowed_groups = array(5, 7, 8); // 可配置为后台设置

	$message = preg_replace_callback(
		"/\[hide\](.*?)\[\/hide\]/is",
		function($matches) use ($usergroup, $allowed_groups) {
			if (in_array($usergroup, $allowed_groups)) {
				return '<div class="hidecontent">' . trim($matches[1]) . '</div>';
			} else {
				return '<div class="hidecontent-tip" style="color:red;">此内容仅对特定用户组可见</div>';
			}
		},
		$message
	);
}

 

说明:

$_G['groupid'] 获取当前用户组 ID。
$allowed_groups 数组定义允许直接查看隐藏内容的用户组 ID。
使用 preg_replace 移除 [hide] 标签,直接显示内容,或者保留默认提示(需要回复)。
步骤 2:清理缓存
修改完成后,登录 Discuz! 后台,清除缓存以应用更改:

后台 -> 工具 -> 更新缓存 -> 选择“全部”并提交。

阅读剩余
THE END