最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

membership - Allow access on a page to just only a specific member

programmeradmin1浏览0评论

I am trying to create a membership site on WP where each members will have access to only specific pages created for them. I mean just a member is allowed to view content on a page while that page is restricted to others. I just to grant on a member access to a page. I don't know if this is possible with the help any plugin

I am trying to create a membership site on WP where each members will have access to only specific pages created for them. I mean just a member is allowed to view content on a page while that page is restricted to others. I just to grant on a member access to a page. I don't know if this is possible with the help any plugin

Share Improve this question asked Jun 24, 2012 at 9:12 bondoxextrabondoxextra 211 silver badge2 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 1

The same concept as Dominic's (no need of a members plugin) but expanded to use a Meta Box, visible only for Admins, with a dropdown list with all the users (with exceptions).

Code borrowed and adapted from this answer. Put in functions.php:

// List Users
add_action( 'admin_init', 'wpse_33725_users_meta_init' );
// Save Meta Details
add_action( 'save_post', 'wpse_33725_save_userlist' );

function wpse_33725_users_meta_init()
{
    if( current_user_can( 'administrator' ) )
        add_meta_box( 'users-meta', 'Authorized User', 'wpse_33725_users_meta_box', 'page', 'side', 'high' );
}

function wpse_33725_users_meta_box()
{
    global $post;
    $custom = get_post_custom( $post->ID );
    $users = $custom["users"][0];

    // prepare arguments
    $user_args  = array(
        // exclude users from the list using an array of ID's
        'exclude' => array(1),
        // order results by display_name
        'orderby' => 'display_name'
    );
    // Create the WP_User_Query object
    $wp_user_query = new WP_User_Query($user_args);
    // Get the results
    $authors = $wp_user_query->get_results();
    // Check for results
    if ( !empty($authors) )
    {
        // Name is your custom field key
        echo "<select name='users'>";
        echo '<option value=0>All</option>';
        // loop trough each author
        foreach ( $authors as $author )
        {
            $author_id = get_post_meta( $post->ID, 'users', true );
            $author_selected = ( $author_id == $author->ID ) ? 'selected="selected"' : '';
            echo '<option value='.$author->ID.' '.$author_selected.'>'.$author->user_nicename.'</option>';
        }
            echo "</select>";
    } 
    else 
    {
        echo 'No authors found';
    }
}


function wpse_33725_save_userlist()
{
    global $post;

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
    {
        return $post->ID;
    }

    update_post_meta( $post->ID, "users", $_POST["users"] );
}

For checking permissions, this works outside the loop in page.php:

$the_user = get_post_meta( $wp_query->post->ID, 'users', true );

if( '0' == $the_user || empty( $the_user ) ) 
{
    echo "this is a public page";
} 
else 
{
    if( get_current_user_id() == $the_user ) 
        echo "this page is for you";
    else 
    { // NOTHING TO SEE, GO TO FRONT PAGE
        wp_redirect('/');
        header("Status: 302");
        exit;
    }
}

I would also recommend checking out plugins such as paid memberships pro (free), Premise Landing Pages (paid) and restrict content pro (paid). These all could solve your problem.

You could add the IDs of allowed users to a custom field on certain pages. Then use get_current_user_id() and see if it's a match before showing the content.

You can check current user id at the beginning of the page by get_current_user_id() function or you can check more -https://developer.wordpress/reference/functions/get_current_user_id/ and apply condition accordingly.

发布评论

评论列表(0)

  1. 暂无评论