• 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 » How to add multiple user levels by category

How to add multiple user levels by category

Chad Butler · Oct 8, 2012 ·

This post will demonstrate how to restrict content to users based on user levels.  Content could be restricted by category, by tag, or by some other meta (such as a custom field).

This example will demonstrate “level” restriction by category, such as Silver, Gold, and Platinum categories.  In this example, we will set up those levels so that the Gold level users have access to Gold & Silver, and Platinum level users have access to all of the content.

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 filters, wpmem_securify

Welcome to RocketGeek Interactive › Forums › How to add multiple user levels by category

Tagged: filters, wpmem_securify

  • This topic has 14 replies, 6 voices, and was last updated 6 years, 3 months ago by Chad Butler.
Viewing 10 reply threads
  • Author
    Posts
    • October 8, 2012 at 11:46 pm #1438
      Chad Butler
      Keymaster

      This post will demonstrate how to restrict content to users based on user levels.  Content could be restricted by category, by tag, or by some other m
      [See the full post at: How to add multiple user levels by category]

    • December 12, 2012 at 10:40 pm #1679
      davipet
      Participant

      I cannot make this example to work. I created:

      3 new users for the 3 levels
      3 new categories for the 3 levels
      3 new posts each one in one category

      Then I put the code you gave into wp-members-pluggable.php that is in the root plugin directory.

      However, when I login with each of the new users I still can see all the new posts in place of seeing only the one for my level (as defined by the post category).

      Am I missing something? (clearly but what)

      Marco

       

    • December 13, 2012 at 10:27 am #1682
      Chad Butler
      Keymaster

      Hi Marco – I just want to roll back a bit to see if we can figure this out.

      Before you set this up, did you have the plugin installed and working (i.e. you are blocking posts, using the ‘more’ tag to create and excerpt, and you understand how all of that works)?

    • December 13, 2012 at 11:12 am #1684
      davipet
      Participant

      I have the plugin installed and working to block access to people who hasn’t logged in.

      Moreover I use the shortcode to show something for logged users and something else for not logged users in the page.

      Not familiar with the — more — tag also because I never used the plugin with blog posts but only with pages.

      In fact this is the first time I am testing blog posts because your example is about posts and I would need to ask how to do the same for pages. But I wanted to see if it worked first.

      Marco

    • December 13, 2012 at 3:35 pm #1685
      Chad Butler
      Keymaster

      Ahhh,..  with posts, you need to use the ‘more’ tag to create an excerpt or you’ll be able to see the entire post on an archive template (anything that’s not a single page).  The ‘more’ tag is inserted in the post editor window – it’s the little button that has a horizontal line splitting through it.

      (there’s a more descriptive info in the Users Guide here: https://rocketgeek.com/plugins/wp-members/users-guide/managing-content/restricting-posts/ )

      Also, you could try out the 2.8 beta version.  This has an “auto-excerpt” feature for any post that should be blocked but doesn’t have a ‘more’ tag already in it (inserting one will override this).  More info on the 2.8 beta here: https://rocketgeek.com/release-announcements/wp-members-2-8-0-beta-release-available-for-testing/

      Hope that helps – let me know if that corrects the problem (or not).

    • May 29, 2014 at 7:46 am #4996
      mtsuwd
      Participant

      Hi Chad,

      i need this option for static sites, so i have to use a custom field.

      So i will change this line
      $the_cat = get_the_category( $post->ID );

      Should i use get_post_custom(), get_post_custom_values() or get_post_custom_keys() ?

      Thanks in advanced

      Marc

      • May 29, 2014 at 5:17 pm #4997
        Chad Butler
        Keymaster

        Hi Marc,

        I think I understand what you are doing – using a custom field to give your posts some unique group identifier. Do I have that right?

        I’m pretty sure you could use any one of the functions you listed as long as it returns a value you can test for using some type of “if” statement or a PHP switch/case.

        Hope that helps.

    • July 7, 2014 at 10:36 pm #5332
      Jason
      Participant

      Chad, thank you for your excellent plugin and equally thorough documentation.

      I’ve looked over your example here about restricting posts and I believe I’m beginning to understand it. Now, I would like to restrict certain pages by user access level instead of posts. In my case I have two levels, Managers and Employees, and there are certain pages that I only need managers to be able to see based on their role or user level.

      I’ve installed the WP Members User List plugin and I have a role field already set up containing the proper values. The last step is to indicate to WordPress that only Managers can see Manager-only pages and employees can see everything else. Have you already covered this? If not, can you point me in the right direction?

      In the nested if statements, I think I will need to indicate that I want it to compare the access level of the page instead of the $post.

      I’m not well read yet on the guts of WordPress and I’m looking forward to learning from this.

      • July 7, 2014 at 11:07 pm #5334
        Chad Butler
        Keymaster

        Hi Jason,

        Thanks for your post. I think what might work well for you is the method described in this post.

        This method takes it beyond categories and allows you to restrict any content (either post or page) based on your created levels. And it will add a dropdown selector in the post/page editor for you to easily assign the level to a post or page.

        Hope that helps.

    • July 10, 2014 at 10:44 am #5351
      Omarben
      Participant

      Chad,
      I have this method working: “Restrict content by user level” (Link #1 below)
      I have three levels Level1,Level2,Level3 just like the example
      I have used it to make menus and pages that are restricted by level.
      It is working exactly the way I expected and wanted.
      I have the 2-part registration working. I have accomplished what I sought out to when went looking for a member plugin and selected on wp-members .
      What, was that a week ago? Seems like a long time since my site has come so far. Consider this a ringing endorsement.
      Now that I have my basic functionality I thought I could try some of the OTHER tricks in your bag that are available.
      Here is what I want to do. Create custom blog post pages by subject.
      Then filter which posts are visible on custom blog pages by levels as well as category..
      I added Trick #2 “How to restrict categories to a defined user group”.
      That should allow me to filter what posts users see on custom pages by which category they belong to.
      And just to summarize, Trick #1 Gives filtered access to pages and post by level..
      What if I want users to see other local functions of the mapping of categories?
      Say users in category_a can see category_a and category_b (or also seen as block category_c)
      Users in category_b can see category_b and category_c (or also seen as block category_a)
      Users in category_c can just see category_c
      Is this logical mapping similar to what you are doing in trick # 3 where you map levels to categories so that level 3 can see all three categories?
      Level 2 sees Leve1,2 and level 1 sees only Level1
      So Trick #2 is mapping users categories to categories while Trick #3 is mapping user levels to categories and though aggregate hierarchical mapping (higher level see all lower categories) gives you a way to map more than one category to a user level.
      Is there any limitation on having multiple trick #3’s where a user can belong the two different stacks of levels simultaneously that map to different group of categories?
      This can be tricky to visualize. I tried my best to explain but I understand if this is not clear.

      Thanks,

      1) Restrict content by user level (level1,level2,level3)
      2) How to restrict categories to a defined user group (category_a,category_b,category_c)
      3) How to add multiple user levels by category (gold, silver, platinum)

      • July 11, 2014 at 11:53 am #5354
        Chad Butler
        Keymaster

        I think you are asking about blocking categories and having higher level users be able to view lower level categories, but not the other way around?

        Yes, you can do that. If you take a look at how the logic works in #3 you should be able to apply that to categories.

        I took the category restriction (a, b, c) logic and add applied some additional logic tests to allow higher levels to view lower level content (with c being the highest, a being the lowest). So a, b, & c can view a; b & c can view b, and only c can view c. I think something like this would do it:

        add_filter( 'wpmem_securify', 'category_access_check' );
        function category_access_check( $content )
        {
        	// array of blocked categories (slugs)
        	$cats = array(
        		'category_a',
        		'category_b',
        		'category_c'
        	);
        	
        	// user category Option Name (meta_key)
        	$user_level = 'user_category';
        		
        	// error message (%s will insert the category name later)
        	$err_msg = 'This content is restricted to %s users and above.';
        	
        	global $post;
        	$the_cat = get_the_category( $post->ID );
        	if( is_user_logged_in() && in_array( $the_cat[0]->slug, $cats ) ) {
        
        		global $user_ID;
        		$user_level = get_user_meta( $user_ID, $user_level, true );
        		$access = false;
        		
        		// if the user level matches the category
        		if( $user_level == $the_cat[0]->slug ) { 
        			$access = true;
        		} else {
        			// do other possibilities
        			switch $cats {
        				case 'category_a':
        					// if the user has a user level, grant access
        					$access = ( in_array( $user_level, $cats ) ) ? true : false;
        					break;
        				
        				case 'category_b':
        					$access = ( $user_level == 'category_b' || $user_level == 'category_c' ) ? true : false;
        					break;
        			}
        		}
        		
        		// handle content based on level (either access or error message
        		return ( $access ) ? $content : sprintf( $err_msg, $the_cat[0]->name );
        		
        	} 
        
        	// anything else is blocked or unblocked on regular plugin logic
        	return $content;
        }
    • November 7, 2014 at 2:44 pm #6365
      andiconti
      Participant

      Where do I paste the filter code? Do I need to create a new file?

      Oh, and I know that when I registered I put clientsfirst-ax.com
      but you can view the test site at whatcanerpdoforyou.com

      Thank you,

      • November 7, 2014 at 3:05 pm #6367
        Chad Butler
        Keymaster

        Generally, if you see that something is a filter or an action (add_filter or add_action), it will go in your theme’s functions.php file.

        If you like to keep things neat, you can create a new file and then call it as an include in functions.php – but you don’t have to do that.

    • November 11, 2014 at 10:19 am #6381
      andiconti
      Participant

      Hi Chad,

      I’m not much for coding, but I really like your plug-in because it is light and easy!
      I am working w/ kriesi enfold theme that has portfolio entries (you can see how this works w/ s2member framework at clientsfirst-tx.com/resources/video-portal). I’m wanting to block certain individual portfolio entries.

      I essentially just want to block portfolio entries that have a certain category checked.

      When I used the code set and tested, it blocked all the pages. Ideally, I would like other way around where when a portfolio entry has category ‘member’ checked, it is restricted. If restricted, then I would also like a link to register.

      I saw the reply you left for Omarben, would this work for what I’m trying to do?

      This may be beyond the scope of our forum, but I am willing to discuss other options for getting help.

      Thank you Chad,
      Andi Conti

    • November 11, 2014 at 2:27 pm #6385
      Chad Butler
      Keymaster

      Actually, I think this is a little off topic both here and over here. Both of these threads are more dealing with creating some kind of levels of restriction. I think what you are looking for is to just block content based on what category it is in, is that right?

      At the most basic level, here is how to create category blocking.

  • Author
    Posts
Viewing 10 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