许多站长最头疼的就是,用户疯狂发表文章,并且是垃圾广告类型的文章,这样会导致管理员运营网站非常困难,今天给大家带来的就是WordPress控制用户发表文章频率,管理员不包含在内!
可将以下代码添加到主题的functions.php文件中:
//设置用户发表文章的时间间隔
function restrict_posting($atts, $content = null) {
//获取当前用户ID
$user_id = get_current_user_id();
//如果是管理员则不限制时间间隔
if(current_user_can('manage_options')) {
return;
}
//设置时间间隔为1天
$time_interval = 86400;
//获取用户上一次发表文章的时间
$last_post_time = get_user_meta($user_id, 'last_post_time', true);
//如果用户未发表过文章,则上一次发表时间为当前时间减去时间间隔
if(!$last_post_time) {
$last_post_time = time() - $time_interval;
}
//计算距离上一次发表文章的时间间隔
$time_since_last_post = time() - $last_post_time;
//如果时间间隔小于设定值,则显示错误信息
if($time_since_last_post < $time_interval) {
$error_message = '您发表文章的时间间隔太短,请等待一天后再发表新的文章。';
return '<div class="error">' . $error_message . '</div>';
}
//更新用户上一次发表文章的时间
update_user_meta($user_id, 'last_post_time', time());
}
//将函数与WordPress的“publish_post”动作挂钩
add_action('publish_post', 'restrict_posting');
该代码将限制普通用户发表文章的时间间隔为1天,管理员除外。如果用户在1天内尝试发表多篇文章,则会显示错误信息。可以根据需要调整时间间隔和错误信息的内容。
修改间隔时间:
- //设置时间间隔为1天 $time_interval = 86400;
- 这里的86400秒=一天,根据自身情况修改间隔时间。
插件控制发帖频率:

v1.0.0
WordPress Post Time Limit 控制用户发表文章频率插件
© 版权声明
本资源由用户投稿上传,内容来自互联网,本站只做免费推荐用于学习分享,如有版权及其他问题,请点击《侵权处理》
THE END
暂无评论内容