Share

wordpress用户只有在登录的情况下才能查看文章及资料的代码插件

<?php
/*
Plugin Name: 限制所有内容访问插件
Description: 仅允许登录用户查看所有内容
Version: 1.0
Author: a5.org.cn
*/
// 检查用户是否登录
function restrict_all_content_access() {
    // 如果用户未登录,则隐藏所有内容
    if (!is_user_logged_in()) {
        add_filter('the_content', 'restrict_all_content_hide');
    }
}
// 隐藏所有内容的回调函数
function restrict_all_content_hide($content) {
    $message = '请登录后查看内容。'; // 可自定义显示的消息
    $content = '<p>' . $message . '</p>';
    return $content;
}
add_action('template_redirect', 'restrict_all_content_access');
?>
<?php
/*
Plugin Name: 限制访问首页插件
Description: 仅允许登录用户访问首页
Version: 1.0
Author: a5.org.cn
*/
// 检查用户是否登录
function restrict_homepage_access() {
    // 如果用户未登录且当前访问的是首页,则重定向到登录页面
    if (!is_user_logged_in() && is_front_page()) {
        auth_redirect();
    }
}
add_action('template_redirect', 'restrict_homepage_access');
?>
//不懂代码的不要轻易尝试使用,显示重定向太多而无法打开网站
<?php
/*
Plugin Name: 限制文章和资料访问
Description: 仅允许登录用户查看特定文章和资料
Version: 1.0
Author: a5.org.cn
*/
// 检查用户是否登录
function restrict_content_access() {
    // 如果用户未登录,则重定向到登录页面
    if (!is_user_logged_in()) {
        auth_redirect();
    }
    // 获取当前文章或页面的ID
    $post_id = get_queried_object_id();
    // 定义允许访问的文章和资料ID
    $allowed_posts = array(1, 2, 3); // 这里的数字是文章或资料的ID
    // 如果当前文章或资料不在允许访问列表中,则重定向到其他页面(例如首页)
    if (!in_array($post_id, $allowed_posts)) {
        wp_redirect(home_url());
        exit;
    }
}
add_action('template_redirect', 'restrict_content_access');
?>

版权申请:本文A5资源网原创,经原创作者允许转载许可声明。原文地址http://a5.org.cn/a5_ziyuan/38974.html