如何限制内容只有特定会员才能阅读
Published
2023-09-09
浏览次数 : 28
function restrict_content_by_role($content) {
if (is_single()) { // Check if it's a single post
$post_id = get_the_ID(); // Get the current post ID
// Get the custom field value "restrict_access" for the current post
$restrict_access = get_post_meta($post_id, 'restrict_access', true);
if ($restrict_access === 'yes') { // Check if the post is set to restrict
// Check if user is logged in
if (is_user_logged_in()) {
// Get the current user's role
$user = wp_get_current_user();
$user_roles = $user->roles;
// Define the roles that can access the content
$allowed_roles = array('editor', 'administrator');
// Check if the user's role is allowed
if (array_intersect($user_roles, $allowed_roles)) {
// User has an allowed role, return the original content
return $content;
} else {
// User does not have an allowed role, display restricted message
return 'This content is restricted. You do not have permission to access it.';
}
} else {
// User is not logged in, display login message
return 'Please login to access this content.';
}
}
}
// Return content if it's not a single post or if the post is not set to restrict
return $content;
}
add_filter('the_content', 'restrict_content_by_role');
完整版
<?php
/**
* 限制用户阅读帖子内容
* 在文章字段里设置限制的话文章只有登录后并且只有特定角色的用户才能阅读
*/
function add_custom_restrict_field_meta_box() {
add_meta_box(
'custom_restrict_field_meta_box',
'Restrict Post Content',
'display_custom_restrict_field_meta_box',
'post',
'normal',
'high'
);
}
function display_custom_restrict_field_meta_box($post) {
// Retrieve the current value of the custom field
$restricted = get_post_meta($post->ID, '_custom_restrict_field', true);
// Output the HTML for the custom field
?>
<label for="custom_restrict_field">
<input type="checkbox" id="custom_restrict_field" name="custom_restrict_field" value="1" <?php checked($restricted, '1'); ?> />
Restrict this post's content
</label>
<?php
}
function save_custom_restrict_field($post_id) {
// Save the custom field value
if (isset($_POST['custom_restrict_field'])) {
update_post_meta($post_id, '_custom_restrict_field', '1');
} else {
delete_post_meta($post_id, '_custom_restrict_field');
}
}
add_action('add_meta_boxes', 'add_custom_restrict_field_meta_box');
add_action('save_post', 'save_custom_restrict_field');
function restrict_content_by_role($content) {
global $post;
$restricted = get_post_meta($post->ID, '_custom_restrict_field', true);
if ($restricted == '1') {
// code...
if (is_user_logged_in()) {
// code...
// Get the current user's role
$user = wp_get_current_user();
$user_roles = $user->roles;
// Define the roles that can access the content
$allowed_roles = array('editor', 'administrator');
// Check if the user's role is allowed
if (array_intersect($user_roles, $allowed_roles)) {
// User has an allowed role, check the custom field
// Post is restricted, display restricted message
return 'This content is restricted. You do not have permission to access it.';
} else {
// User does not have an allowed role, display restricted message
return '你需要特定的用户权限才能查看,请提升用户权限';
}
} else {
// User is not logged in, display login message
return '请 <a href="' . esc_url(wp_login_url()) . '"> 登录 </a>查看内容.';
}
} else {
return $content;
}
}
add_filter('the_content', 'restrict_content_by_role');
- 标签1
- 标签1
- 标签1