• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Store
    • Cart
    • Checkout
  • Blog
    • Basics [Free]
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Contact
  • Sign In
  • Show Search
Hide Search
Home » Filters » Restrict Post or Page Access to a Specific User

Restrict Post or Page Access to a Specific User

Chad Butler · Dec 20, 2013 ·

Every once in awhile, I get a question about restricting content (either page or post) to a specific user.  There is a limited way to do that with the wpmem_securify filter, similar to the method for blocking content by category and user level.

post_meta_with_user_dropdownThis process is actually easier than blocking by levels or categories because you only need to determine three things:

  1. Is the user logged in? If not, then it is blocked regardless.
  2. If the user is logged in, but not the specified user, it is blocked and an error message is given.
  3. If the user is logged in and is the specified user, then the content is delivered.

This example will make use of a couple of new action hooks that will allow us to add a dropdown selector of users to the WP-Members post meta box.  

This article is only available to WP-Members Support Subscribers. If you have an existing subscription, please login below. If you do not have a current support subscription, you can purchase a support subscription here.

Already a Member? Log In Here
   
Forgot password? Click here to reset

To gain full access to WP-Members premium content, a current support subscription is required. You can purchase an annual support subscription for as little as $59, which provides you with access to priority support, a customer support forum, access to code snippets, and more.

Why wait? Choose your subscription option here.

[
Why join?]

Filters, Tips and Tricks wpmem_admin_after_block_meta, wpmem_admin_block_meta_save, wpmem_securify

Welcome to RocketGeek Interactive › Forums › Restrict Post or Page Access to a Specific User

Tagged: wpmem_admin_after_block_meta, wpmem_admin_block_meta_save, wpmem_securify

  • This topic has 15 replies, 5 voices, and was last updated 6 years, 2 months ago by Chad Butler.
Viewing 6 reply threads
  • Author
    Posts
    • December 20, 2013 at 11:44 am #3689
      Chad Butler
      Keymaster

      Every once in awhile, I get a question about restricting content (either page or post) to a specific user.  There is a limited way to do that with the
      [See the full post at: Restrict Post or Page Access to a Specific User]

    • March 29, 2014 at 3:38 pm #4512
      toniM
      Participant

      Is there a way to give more than one User access? Or always give Administrator access to all pages and posts?

      • March 29, 2014 at 7:21 pm #4513
        Chad Butler
        Keymaster

        Hi Toni,

        I think if you change the logical test at the end to something like this:

        ($ post_access != $user_ID || current_user_can( 'administrator' ) )

        that should allow you to limit to a specific user or administrator.

        I can probably come up with a way to do something for a specific array of users, but I’d have to work on that when I get back on Friday.

        You can estrict based on different criteria for larger groups (such as users assigne a certain meta value) with something like what is dscribed here:

        https://rocketgeek.com/filter-hooks/how-to-add-multiple-user-levels-by-category/

        Hope that helps. Feel free to drop me a message through the contact form if you need more details or need to expand on these ideas in a more specific way. That wa it will be in the queue when I get back on Friday.

      • December 23, 2014 at 10:04 am #6652
        Chad Butler
        Keymaster

        I’ve recently added a twist on this idea with a multiple select field for selecting multiple users.

    • March 30, 2014 at 12:59 pm #4514
      toniM
      Participant

      Tried changing/adding the code but did not work, can only access page as single user. Maybe I put in the wrong place

      // if the user is the user this page is restricted to, return the
      // $content unfiltered. Otherwise, return the error message
      		return ( $post_access != $user_ID || current_user_can( 'administrator' )) ? $error_msg : $content;	
      	}
      	// return unfiltered content for all other cases
      	return $content;
      }
      

      Tried the multiple user example and managed to restrict access to everyone! Maybe part of problem is I am using Pages, not Posts.

      • March 30, 2014 at 4:14 pm #4515
        Chad Butler
        Keymaster

        Hi Toni

        Just doing my final check in before i have to sign off. Shoot me a message through the contact form so it’s in my queue when I get back. I’ll be able to test something for you then.

      • April 7, 2014 at 8:48 am #4579
        Palmer Hargreaves
        Participant

        I am working on something very similar (i.e.multiple users with access) and I was looking on the site today to see if there was anything that could help me with this. My solution to the above would be to either have a single custom field that maintains a delimited list of allowed userids or to have an Advanced Custom Fields repeater field

    • April 18, 2014 at 4:24 am #4679
      Palmer Hargreaves
      Participant

      As an update to my situation with this which is as follows

      My Use Case is of a custom post type that is a hierarchy of Pages.

      These pages represent information on projects and have levels for different aspects of the project.

      A ‘member’ level user may be granted access to any level of a project and all sub levels to that node.

      My solution has been to create a custom table of member_in_project that contains two fields
      memberID = The User ID of the member and
      projectID = The postID of the node at which I allow access.

      Thereafter when a member goes to the Projects section he is presented with a list of links to the postIDs for all the nodes to which he is authorised.

      I have also created a custom single page template for my custom post type that will list the children to that page.

      For Navigation I have a breadcrumb trail at the top of the page that takes the user up the hierarchy back to the level at which they were granted access.

      I have it in mind to build in some checking to make sure a user can visit a page by walking back up the tree and checking to see if they have rights at any postID if so then they get the content otherwise they are disallowed.

      There is a producer member type that has the ability to assign and remove rights for a member.

      Right now it is working fine however we are going live with it next week so more news then but as a strategy I believe this works OK and it might do so for others.

      • April 18, 2014 at 9:58 am #4680
        Chad Butler
        Keymaster

        Hi Palmer,

        That’s really good information – thanks for sharing it. I really enjoy hearing how people are extending and customizing the use of the plugin in new and different ways.

        You may already know this, but your mention of the heirarchy access and checking made me think of this particular post I did a long time ago about blocking child pages. This uses get_post_ancestors to check access of the parent page to see if access is allowed. I don’t know if it helps in your situation, but thought I’d mention it just in case.

    • May 8, 2014 at 8:50 am #4831
      MooshiMode
      Participant

      Hi

      How do I create a single multiple-user level by category i.e. one level rather than multiple levels of access. I want to sell online tutorials to subscribing members and don’t want to create a hierarchy of levels. Can I change the code in the snippet for https://rocketgeek.com/filter-hooks/how-to-add-multiple-user-levels-by-category/?

      This multiple user level would only be accessible on two conditions:

      – as a paying member;
      – and the member bought the tutorial.

      • May 8, 2014 at 5:46 pm #4832
        Chad Butler
        Keymaster

        I think all you need here is the PayPal extension. You don’t have to set up levels for that if you just want to restrict it based on whether they’ve paid or not.

    • May 8, 2014 at 9:09 pm #4833
      MooshiMode
      Participant

      Hi Chad, not sure that will work for me. I want to create another level of restrictions within the membership that restricts a tutorial page or post according to whether a member bought a downloadable product from me. Their general membership subscription will not allow access to these tutorial pages or posts unless they bought this product.

      Can I create a multiple choice checklist Field following your method on how to restrict categories for a defined user group? I’d like to be able to select multiple categories for a user as they buy more tutorials from me.

      Olivia

      • May 9, 2014 at 10:07 am #4835
        Chad Butler
        Keymaster

        Hi Olivia – I’m going need to think about this a little bit. My initial thought is to add a feature to the PayPal extension to allow for purchasing access to specific content. That I think could be implemented into the current framework. But I need to think about how to implement that.

    • November 9, 2014 at 5:59 pm #6371
      Shillos
      Blocked

      Can this be done by restricting by user role instead by a specific user?!

      • November 9, 2014 at 6:25 pm #6372
        Chad Butler
        Keymaster

        Yes – and since I don’t really have anything specific to restricting content by roles, that probably deserves its own post. I’ll put something together for that.

        • November 11, 2014 at 3:59 pm #6391
          Chad Butler
          Keymaster

          So… I’ve added a discussion of building a similar process that restricts by user role. See: https://rocketgeek.com/filter-hooks/restrict-a-post-or-a-page-to-a-specific-user-role/

  • Author
    Posts
Viewing 6 reply threads
  • You must be logged in to reply to this topic.
Log In

Ready to get started?

Join Today!

© 2021 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Refund Policy