Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author myCred

    (@designbymerovingi)

    Hi

    Just as you can restrict access to content on your website for any other reason, you can also do so based on a users balance. When you hook into WordPress to apply your restrictions, you can always get the current users points balance though the mycred_get_users_cred function.

    Thread Starter autowakins

    (@autowakins)

    Thanks for the quick response, Gabriel (always been fantastic).

    Would you mind showing me how to create that restriction hook? Or show me a link to the tutorial.

    Thanks
    W.

    Plugin Author myCred

    (@designbymerovingi)

    Well it really depends on how you want to approach this. There are plenty of plugins out there who can make content restriction easy for you but you can also build your own in case you are looking for something simple though different means.

    A very basic example would be to use the template_redirect or wp action hooks where you have access to WP conditional tags.

    Lets say you want to restrict access to a page called “Premium Content” for those who does not have at least 100 points by redirecting them to “Restricted” page. You could do it in the following way:

    add_action( 'wp', 'restrict_access_based_on_points' );
    function restrict_access_based_on_points()
    {
    	// Ignore admin area
    	if ( is_admin() ) return;
    
    	// Check if this is the premium content page
    	if ( is_page( 'premium-content' ) ) {
    		// Get users balance
    		$users_balance = mycred_get_users_cred();
    		// Check if it is less then 100 and that we are not admins
    		if ( $users_balance < 100 && !mycred_is_admin() ) {
    			wp_safe_redirect( home_url( '/restricted/' ) );
    			exit;
    		}
    	}
    }

    You could also do this though the template_redirect action hook but wp is the earliest instance you can use conditional tags ( is_page() ). As you can see in the code, we redirect everyone with a balance lower then 100 unless they are admins.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to Restrict Page Content Per User Level/ Points?’ is closed to new replies.