此代码仅可以对文章ID范围进行修改,例如文章id:1-100 的范围,无法对指定分类进行修改。其实比较鸡肋,但对于一段时间一直写同一种类型文章的批量权限有点帮助。
但这是春哥给别人写的,我觉得比较保险。
我尝试了将此代码修改为分类下的文章修改,放在文章末尾
使用方法:
- 设置好你要修改的文章id范围,然后设置你要修改的权限,只能单独一个权限
- 将此代码放到子主题,page目录下创建一个php文件将代码放进去,打开浏览器访问这个php文件即可。
- 访问地址示例:www.xxxx.cn/wp-content/themes/b2child/Pages/aaa.php
<?php
/**
* 使用此代码前,先备份一下数据库,以免出现意外造成数据损坏。
* 此代码复制到 Pages/test.php 中保存,然后使用 你的网址+/test 去访问一下,显示成功后再删掉保存即可。
*/
// 加载WordPress核心环境(关键修复)
// 请确认 ABSPATH 的路径是否正确,以下是标准路径,若你的网站目录结构不同需调整
define('WP_USE_THEMES', false);
require_once('../../../../wp-load.php');
// 防止非管理员访问(可选,增加安全性)
if (!current_user_can('administrator')) {
wp_die('你没有权限访问此页面!');
}
// 原始业务逻辑
$arg = array(
'post_type'=>'post',//文章形式是post
'post_status'=>'publish',//文章状态是已发布
'posts_per_page'=>-1//所有文章,如果文章数量过多(几万篇),可能速度很慢
);
$the_query = new WP_Query($arg);
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$id = get_the_id();
$res = false;
//要设置的文章ID范围
if($id >=1184 && $id <= 1349){
$downs = get_post_meta($id,'b2_single_post_download_group',true);
if(!empty($downs)){
$save = false;
foreach ($downs as $k => $v) {
if(isset($downs[$k]['rights'])){
$downs[$k]['rights'] = 'all|free';
$save = true;
}
}unset($v);
if($save){
update_post_meta($id,'b2_single_post_download_group',$downs);
}
}
}
}
echo '完成';
wp_reset_postdata();
} else {
echo '没有文章';
}
// 结束执行
exit;
?>
通过分类ID批量修改指定分类下的文章下载权限|使用方法同上
在你拿走这个功能时,我想告诉你一点我的想法:
1、在你用此代码之前,我希望你一定要备份好自己的网站数据库后,再进行修改
2、我希望熬的这3个小时成果有帮到你,而不是换来你的恶语相向
3、代码我测试了没问题,但我无法对你的数据进行保障,如果你没有任何代码基础,也对我并不放心,希望你停止使用
1、在你用此代码之前,我希望你一定要备份好自己的网站数据库后,再进行修改
2、我希望熬的这3个小时成果有帮到你,而不是换来你的恶语相向
3、代码我测试了没问题,但我无法对你的数据进行保障,如果你没有任何代码基础,也对我并不放心,希望你停止使用
<?php
/**
* WordPress 自定义字段批量修改工具(光标行精准修改版)
* 核心:按钮修改光标所在行内容,而非最后一行
*/
// 加载WordPress核心环境
define('WP_USE_THEMES', false);
require_once('../../../../wp-load.php');
// 1. 管理员权限验证
if (!current_user_can('administrator')) {
wp_die('你没有权限访问此页面!仅管理员可操作。');
}
// 2. 处理表单提交逻辑
$submit_result = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['batch_modify_submit'])) {
// CSRF安全验证
if (!wp_verify_nonce($_POST['batch_modify_nonce'], 'batch_modify_action')) {
$submit_result = '<div class="alert error">⚠️ 安全验证失败!请刷新页面重试。</div>';
} else {
// 接收并过滤输入
$target_category_id = intval($_POST['category_id']);
$rights_text = sanitize_textarea_field($_POST['rights_value']);
// 基础验证
if ($target_category_id <= 0) {
$submit_result = '<div class="alert error">⚠️ 分类ID必须是大于0的数字!</div>';
} elseif (empty(trim($rights_text))) {
$submit_result = '<div class="alert error">⚠️ 权限值不能为空!</div>';
} else {
// 处理多行文本为带\r\n的字符串(匹配数据库格式)
$rights_lines = preg_split('/\r\n|\r|\n/', $rights_text);
$valid_lines = [];
foreach ($rights_lines as $line) {
$line = trim($line);
if (!empty($line)) {
$valid_lines[] = $line;
}
}
$target_rights = implode("\r\n", $valid_lines);
// 执行批量修改
$arg = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'no_found_rows' => true,
'cat' => $target_category_id,
'fields' => 'ids',
);
$post_ids = get_posts($arg);
$total_posts = count($post_ids);
$has_field_posts = 0;
$updated_posts = 0;
$log = [];
if ($total_posts > 0) {
foreach ($post_ids as $post_id) {
$downs = get_post_meta($post_id, 'b2_single_post_download_group', true);
$log[] = "文章ID {$post_id}:";
if (empty($downs)) {
$log[] = " → 无b2_single_post_download_group字段 → 跳过";
continue;
}
$has_field_posts++;
$save = false;
foreach ($downs as $k => $v) {
if (isset($downs[$k]['rights'])) {
$old_value = $downs[$k]['rights'];
$downs[$k]['rights'] = $target_rights;
$save = true;
// 日志优化:换行符转为可视化的「↵」
$log_old = str_replace("\r\n", " ↵ ", $old_value);
$log_new = str_replace("\r\n", " ↵ ", $target_rights);
$log[] = " → 原rights值:{$log_old}";
$log[] = " → 新rights值:{$log_new}";
}
}
if ($save) {
update_post_meta($post_id, 'b2_single_post_download_group', $downs);
$updated_posts++;
$log[] = " → 保存成功";
} else {
$log[] = " → 无rights字段 → 跳过";
}
}
// 结果展示
$show_rights = str_replace("\r\n", " ↵ ", $target_rights);
$submit_result = '
<div class="alert success">✅ 执行完成!
<ul>
<li>目标分类ID:'.$target_category_id.'</li>
<li>目标权限值(共'.count($valid_lines).'行):'.$show_rights.'</li>
<li>分类下已发布文章总数:'.$total_posts.'</li>
<li>包含目标字段的文章数:'.$has_field_posts.'</li>
<li>成功修改的文章数:'.$updated_posts.'</li>
</ul>
<div class="log">
<strong>详细日志(↵代表换行):</strong><br>
'.implode('<br>', $log).'
</div>
</div>';
} else {
$submit_result = '<div class="alert warning">⚠️ 分类ID '.$target_category_id.' 下没有找到已发布的文章!</div>';
}
}
}
}
// 3. 前台表单界面(光标行精准修改)
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>B2主题文章下载权限批量修改工具</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 0 20px; line-height: 1.6; }
.form-container { background: #f5f5f5; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
.form-group { margin-bottom: 20px; }
label { display: block; margin-bottom: 8px; font-weight: bold; color: #333; }
input[type="number"] { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; }
textarea { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; min-height: 150px; resize: vertical; }
button { cursor: pointer; border: none; border-radius: 4px; font-size: 14px; padding: 8px 16px; margin: 0 5px 8px 0; }
.submit-btn { background: #21759b; color: white; padding: 12px 30px; font-size: 16px; margin-top: 10px; }
.submit-btn:hover { background: #135e87; }
.prefix-btn { background: #4285f4; color: white; }
.prefix-btn:hover { background: #3367d6; }
.suffix-btn { background: #0f9d58; color: white; }
.suffix-btn:hover { background: #0b8043; }
.clear-btn { background: #db4437; color: white; }
.clear-btn:hover { background: #c53929; }
.btn-group { margin: 10px 0; }
.btn-group label { display: inline-block; margin-right: 10px; font-weight: normal; color: #666; }
.alert { padding: 15px; border-radius: 4px; margin-top: 20px; }
.success { background: #d4edda; color: #155724; }
.error { background: #f8d7da; color: #721c24; }
.warning { background: #fff3cd; color: #856404; }
.log { margin-top: 10px; padding: 10px; background: #fff; border-radius: 4px; font-size: 14px; }
.hint { font-size: 14px; color: #666; margin-top: 5px; }
</style>
</head>
<body>
<div class="form-container">
<h2>B2主题文章下载权限批量修改工具</h2>
<p>说明:修改前备份数据库!不仅要服务器备份,拖到你电脑本地一份!避免修改数据酿成大错!</p>
<p>我一直认为改数据是很重要的事情,如果有免费的快照,大改之前去服务商那里备份一个服务器快照!</p>
<form method="post" action="">
<?php wp_nonce_field('batch_modify_action', 'batch_modify_nonce'); ?>
<!-- 分类ID输入 -->
<div class="form-group">
<label for="category_id">目标分类ID(必填)分类目录id去你的文章分类目录查看,第一排的数字ID就是</label>
<input type="number" id="category_id" name="category_id" min="1" placeholder="例如:5" required>
</div>
<!-- 权限值输入 + 快捷按钮 -->
<div class="form-group">
<label for="rights_value">文章权限值(必填,一行一个)写完回车换行</label>
<textarea id="rights_value" name="rights_value" placeholder="例如:
all|free
lv1|credit=1
vip0|free" required><?php echo isset($_POST['rights_value']) ? esc_textarea($_POST['rights_value']) : "all|free
lv|credit=1
vip0|free"; ?></textarea>
<!-- 前缀按钮组 -->
<div class="btn-group">
<label>前缀(用户类型):</label>
<button type="button" class="prefix-btn" data-value="all|">所有</button>
<button type="button" class="prefix-btn" data-value="lv|">普通用户</button>
<button type="button" class="prefix-btn" data-value="vip|">VIP</button>
<button type="button" class="prefix-btn" data-value="guest|">游客</button>
</div>
<!-- 后缀按钮组 -->
<div class="btn-group">
<label>后缀(权限类型):</label>
<button type="button" class="suffix-btn" data-value="comment">评论可见</button>
<button type="button" class="suffix-btn" data-value="login">登录可见</button>
<button type="button" class="suffix-btn" data-value="credit=">积分(需补数值)</button>
<button type="button" class="suffix-btn" data-value="money=">付费(需补数值)</button>
<button type="button" class="suffix-btn" data-value="free">免费</button>
</div>
<!-- 辅助按钮 -->
<div class="btn-group">
<button type="button" class="clear-btn" id="clearText">清空文本框</button>
<div class="hint">提示:1. 按钮修改光标所在行内容;2. 积分/付费后缀需手动补充数值(如credit=10);3. 自动过滤空行</div>
</div>
</div>
<!-- 提交按钮 -->
<button type="submit" name="batch_modify_submit" class="submit-btn">提交修改</button>
</form>
<!-- 提交结果展示 -->
<?php if ($submit_result) echo $submit_result; ?>
</div>
<script>
// 核心逻辑:精准修改光标所在行内容
document.addEventListener('DOMContentLoaded', function() {
const textarea = document.getElementById('rights_value');
// 1. 前缀按钮点击事件(修改光标行前缀)
document.querySelectorAll('.prefix-btn').forEach(btn => {
btn.addEventListener('click', function() {
const prefix = this.dataset.value;
updateCurrentLine('prefix', prefix);
});
});
// 2. 后缀按钮点击事件(修改光标行后缀)
document.querySelectorAll('.suffix-btn').forEach(btn => {
btn.addEventListener('click', function() {
const suffix = this.dataset.value;
updateCurrentLine('suffix', suffix);
});
});
// 3. 清空文本框按钮
document.getElementById('clearText').addEventListener('click', function() {
textarea.value = '';
textarea.focus();
});
// 核心函数1:获取光标所在行的信息
function getCursorLineInfo() {
const cursorPos = textarea.selectionStart;
const content = textarea.value;
// 分割所有行(兼容\r\n、\r、\n)
const lines = content.split(/\r\n|\r|\n/);
// 计算光标所在行号和该行内容
let currentLineIndex = 0;
let charCount = 0;
for (let i = 0; i < lines.length; i++) {
// 每行的字符数 + 换行符长度(\n是1,\r\n是2)
const lineLength = lines[i].length + (content.includes('\r\n') ? 2 : 1);
if (charCount + lineLength > cursorPos) {
currentLineIndex = i;
break;
}
charCount += lineLength;
}
return {
lineIndex: currentLineIndex, // 光标所在行索引
lineContent: lines[currentLineIndex] || '', // 光标行内容
allLines: lines // 所有行数组
};
}
// 核心函数2:更新光标所在行内容
function updateCurrentLine(type, value) {
const { lineIndex, lineContent, allLines } = getCursorLineInfo();
let newLineContent = lineContent.trim();
// 处理前缀/后缀替换
if (type === 'prefix') {
// 替换前缀:分割|,保留后缀,替换前缀
const parts = newLineContent.split('|');
if (parts.length > 1) {
newLineContent = value + parts[1]; // 保留原有后缀
} else {
newLineContent = value; // 无后缀则仅添加前缀
}
} else if (type === 'suffix') {
// 替换后缀:分割|,保留前缀,替换后缀
const parts = newLineContent.split('|');
if (parts.length > 0 && parts[0]) {
newLineContent = parts[0] + '|' + value; // 保留原有前缀
} else {
newLineContent = 'all|' + value; // 无前缀则默认all|
}
}
// 更新光标行内容
allLines[lineIndex] = newLineContent;
// 重新拼接所有行(用\n兼容所有编辑器,提交时会自动转为\r\n)
textarea.value = allLines.join('\n');
// 恢复光标位置到该行末尾
// 计算新光标位置
let newCursorPos = 0;
for (let i = 0; i < lineIndex; i++) {
newCursorPos += allLines[i].length + 1; // +1是\n的长度
}
newCursorPos += newLineContent.length;
// 聚焦并定位光标
textarea.focus();
textarea.selectionStart = textarea.selectionEnd = newCursorPos;
// 滚动到光标位置
textarea.scrollTop = textarea.scrollHeight;
}
});
</script>
</body>
</html>
1、本站上的所有软件和资料均为软件作者提供和网友推荐收集整理而来,仅供学习和研究使用。如有侵犯你版权的,请来信指出,本站将立即改正。
2、访问本站的用户必须明白,【哔哔一二】对提供下载的软件等不拥有任何权利,其版权归该下载资源的合法拥有者所有。
3、本站保证站内提供的所有可下载资源(软件等等)都是按“原样”提供,本站未做过任何改动;但本网站不保证本站提供的下载资源的准确性、安全性和完整性;同时本网站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。
2、访问本站的用户必须明白,【哔哔一二】对提供下载的软件等不拥有任何权利,其版权归该下载资源的合法拥有者所有。
3、本站保证站内提供的所有可下载资源(软件等等)都是按“原样”提供,本站未做过任何改动;但本网站不保证本站提供的下载资源的准确性、安全性和完整性;同时本网站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。