/**
* Shortcode to restrict content to specific roles.
* Usage: [restricted_content roles="subscriber,editor"]Secret Stuff[/restricted_content]
*/
function wp_pro_restricted_content_shortcode($atts, $content = null) {
// 1. Parse attributes
$atts = shortcode_atts(array(
'roles' => 'administrator', // Default fallback
'message' => '<div class="alert alert-warning">You do not have permission to view this content. Please login or upgrade.</div>'
), $atts);
// 2. Check if user is logged in
if (!is_user_logged_in()) {
return $atts['message'];
}
// 3. Get current user and their roles
$current_user = wp_get_current_user();
$allowed_roles = explode(',', $atts['roles']);
// Clean up spaces in the roles list
$allowed_roles = array_map('trim', $allowed_roles);
// 4. Check if user has one of the allowed roles
$has_permission = array_intersect($allowed_roles, $current_user->roles);
// Always allow Administrator to see everything (Optional, but recommended for debugging)
if (in_array('administrator', $current_user->roles) || !empty($has_permission)) {
// Return the content, processing any shortcodes inside it
return do_shortcode($content);
}
// 5. Return restriction message if logic fails
return $atts['message'];
}
add_shortcode('restricted_content', 'wp_pro_restricted_content_shortcode');
方法二 字段控制方法
function wp_pro_restrict_entire_post($content) {
// Only apply to the main post content loop and single posts
if (!is_singular() || !in_the_loop() || is_admin()) {
return $content;
}
global $post;
// 1. Get the required role from Post Meta
$required_role = get_post_meta($post->ID, '_required_role', true);
// If no rule is set, show content freely
if (empty($required_role) || $required_role === 'all') {
return $content;
}
// 2. Prepare the restriction message
$restriction_message = '<div class="restricted-box">
<h3>Restricted Access</h3>
<p>This content is reserved for <strong>' . ucfirst($required_role) . 's</strong> only.</p>
' . wp_login_form(array('echo' => false)) . '
</div>';
// 3. Logic: Is user logged in?
if (!is_user_logged_in()) {
return $restriction_message;
}
// 4. Logic: Does user match role?
$user = wp_get_current_user();
// Allow Admin to always see or check specific role
if (in_array('administrator', $user->roles) || in_array($required_role, $user->roles)) {
return $content;
}
return $restriction_message;
}
add_filter('the_content', 'wp_pro_restrict_entire_post');
//保存字段
// 1. Register the Meta Box
function wp_pro_add_role_meta_box() {
add_meta_box(
'role_restriction_box', // ID
'Content Access Control', // Title
'wp_pro_render_meta_box', // Callback
'post', // Post type (post, page, custom_type)
'side', // Context
'high' // Priority
);
}
add_action('add_meta_boxes', 'wp_pro_add_role_meta_box');
// 2. Render the HTML for the Box
function wp_pro_render_meta_box($post) {
// Security Nonce
wp_nonce_field('save_role_restriction', 'role_restriction_nonce');
// Get current value
$current_val = get_post_meta($post->ID, '_required_role', true);
// Get all editable roles
global $wp_roles;
$roles = $wp_roles->roles;
echo '<label for="required_role_select">Who can read this?</label>';
echo '<select name="required_role_select" id="required_role_select" style="width:100%; margin-top:5px;">';
echo '<option value="all">Everyone (Public)</option>';
foreach ($roles as $role_key => $role_details) {
$selected = ($current_val === $role_key) ? 'selected' : '';
echo '<option value="' . esc_attr($role_key) . '" ' . $selected . '>' . esc_html($role_details['name']) . '</option>';
}
echo '</select>';
}
// 3. Save the Data
function wp_pro_save_role_meta($post_id) {
// Security checks
if (!isset($_POST['role_restriction_nonce']) || !wp_verify_nonce($_POST['role_restriction_nonce'], 'save_role_restriction')) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!current_user_can('edit_post', $post_id)) return;
// Save or Delete
if (isset($_POST['required_role_select'])) {
update_post_meta($post_id, '_required_role', sanitize_text_field($_POST['required_role_select']));
}
}
add_action('save_post', 'wp_pro_save_role_meta');