1

I followed this guide to start creating a member-based WordPress site for my current client.

They run a property-management business and need the site to act as a portal to connect building owners and tenants of those buildings to the various schematics that show where things like electric meters and sewer drains are. The big issue I'm running into is that none of the plugins I've tried let me fully restrict a page or post to a user (or a couple users).

The plugins I'm using are: Members, Nav Menu Roles, and Profile Builder (free version).

I'm comfortable coding the solution into my theme files, but I'm honestly not sure where to start or what will be the best solution (plugins, coding, or something else??).

Update for clarification

So there will be three main roles: Admin, Building Owner, and Building Tenant (someone who runs a shop at a strip mall, for example).

I am setting up pages for the various buildings, as well as posts that list the different Service Items (Electrical, Fire, HVAC, Garbage, etc.). The goal is for a user to log in and only be able to see the content relating to their building/buildings. So I need to be able to assign those pages and posts to users in the back-end somehow.

I can easily do this according to the user role, but I need to do this according to specific users.

This entire site is basically just for users with accounts, and ideally, the client will want an easy way to assign access on his own once the site is live.

4
  • Hi Kreig, I'm kind of confused about what you are trying to do. There might be a lot of users on the site, what algorithm you want to follow to restrict the access? Are they like VIP users? Users from specific locations? Time? etc? How do you define the privileges for a user?
    – Johansson
    Aug 10, 2017 at 18:31
  • Sorry, I'll update my post to be more specific.
    – KreigD
    Aug 10, 2017 at 18:54
  • No problem :) Just one last question, do the users create the posts? Or you create it for them? And, are you ok with manually assigning every user to a post or should it be automatic?
    – Johansson
    Aug 10, 2017 at 19:13
  • I'm creating the posts. An admin might create posts later, but the other level users never will. Yeah I'm fine with manually assigning access.
    – KreigD
    Aug 10, 2017 at 19:15

1 Answer 1

0

What we can do here is to create a meta box for each post, and assign a user to it. Then, on our post page, check whether the current user is related to the post or not.

If they are not related to it, then redirect them back to homepage or somewhere else.

I'm going to use a drop-down for the meta box, and fill it with users.

// Hook into the add_meta_boxes action
add_action( 'add_meta_boxes', 'kreigd_user_metabox' );
function kreigd_user_metabox() {
    // Create a new metabox
    add_meta_box( 
        'kreigd-user-list',         // ID
        'User List',                // Title
        'kreigd_user_list_callback',    // Callback
        array('post','page'),       // Post types
        'normal', 
        'high' 
    );
}
// Output the content for metabox
function kreigd_user_list_callback() {
    // $post is already set, and contains an object: the WordPress post
    global $post;
    $values = get_post_custom( get_the_ID() );
    // Get the current assigned user
    $user_id = isset( $values['user_id'][0] ) ? $values['user_id'][0] : 0;

    // We'll use this nonce field later on when saving.
    wp_nonce_field( 'kreigd_nonce', 'kreigd_user_list_nonce' );?>
    <p>
        <label for="kreigd_select"><?php _e('Choose a user','text-domain'); ?></label>
        <select name="kreigd_select" id="kreigd_select"><?php 
            $users = get_users(); // Retrieve a list of users
            // Run a loop and add every user to the list
            foreach ( $users as $user ){ ?>
                <option 
                    value="<?php echo esc_html( $user->ID ); ?>"
                    <?php selected( $user->ID, $user_id ); ?>
                >
                    <?php echo esc_html( $user->display_name ); ?>
                </option><?php 
            } ?>
        </select>
    </p><?php    
}
// Save the user to meta box on post save
add_action( 'save_post', 'mymovieflaws_meta_box_save' );
function mymovieflaws_meta_box_save( $post_id ){
    // Don't save it on autosave
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;
    // If the nonce is not correct, or the value is not set, return
    if( !isset( $_POST['kreigd_select'] ) || !wp_verify_nonce( $_POST['kreigd_user_list_nonce'], 'kreigd_nonce' ) ) 
        return;
    // If current user doesn't have the privilege to save a post, return
    if( !current_user_can( 'edit_post', $post_id ) ) 
        return;
    $allowed = array( 'a' => array( 'href' => array() ) );
    // Save the user's ID
    update_post_meta( $post_id, 'user_id', wp_kses( $_POST['kreigd_select'], $allowed ) );      
}

You can utilize this by using the bootstrap select, and add search option to your selector. This way you can quickly find each user, without having to go through the whole list.

4
  • Thank you! That's awesome! A couple questions: how do I make this work for custom post types as well, and would I be ok changing the select into a multi-select (in case two or more users need to be able to view it)?
    – KreigD
    Aug 10, 2017 at 20:25
  • You're welcome! You can change the array that I labeled with //Post type for you, that controls the post types. Also, you can change this into any kind of meta box you want, text field, radio button, etc.
    – Johansson
    Aug 10, 2017 at 20:29
  • I've got a quick question. How do I make the checkboxes stay checked in the meta box after clicking update post? I'm wanting the assigned users to appear checked so an admin knows who can view the post.
    – KreigD
    Aug 14, 2017 at 19:31
  • Hi. There is a function you can use for that purpose. Take a look into the checked() function's documentation.
    – Johansson
    Aug 14, 2017 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.