I want only one No Access Message when post contains more than one shortcode. How?

If there is one or more shortcodes protecting content from being viewed, how can I get the Member plugin to print only one No Access Message to the post?

I have four levels in my setup:

Stranger
Free Registered Member
Paid Member
Paid Premium Member

I have posts that might contain information at all four levels. In other words:

Strangers: they see Post1 - content A for free
Free Registered Members: see Post 1 - content A and B for free
Paid Members: see Post 1 - content A, B, and C for a $$$
Paid Premium Members: see Post 1 - content A, B, C, and D for a $$$

My No Access Message is a link that leads to the registration page where a user can select from the three subscriptions available.

I have this working where the correct content shows depending upon the membership level of the user logged in. My only problem is the "No Access Message": it is printed to the screen once for each of the shortcodes that protect post content. So, when a post contains all four types of content, it has three No Access Messages when viewed by Strangers, two when viewed by Free Registered Members, and one when viewed by Paid Members.

I found a method in the membershippublic class called override_shortcodes() on line 539 that appears to control this using foreach(), but I'm hesitant to modify this. Am I missing an easier way to solve my problem?

  • chasporter

    I just figured out I can do this by nesting shortcodes:

    [shortcode_1]
    foo
    [shortcode_2]
    bar
    [/shortcode_2][/shortcode_1]

    Now the only problem is that there is only one No Access Message possible. It would be better if I could customize the message for each shortcode. For instance, shortcode_1: "Become a member for free!", and shortcode_2: "Become a member for $XX.XX per month".

    Anyone know if the plugin can be easily modified to create individual No Access Messages?

  • DavidM

    Hi chasporter and welcome to WPMU DEV!

    That really is a great idea for a workaround, thanks for posting it! There's not currently a feature in Membership to achieve that but I believe you could simply use the following shortcode to do it!

    function membership_level( $atts, $content = null ) {
    
    	extract( shortcode_atts( array(
          'levelid' => '1',
          'message' => 'No Access!',
          ), $atts ) );
    
    	//also available: current_user_on_subscription()
    	if ( current_user_on_level($levelid) ) {
    		return $content;
    	}
    	else { return $message;}
    }
    add_shortcode('membership-level', 'membership_level');

    And with that in place, perhaps simply included in your theme's functions.php file, you could use the following content in a post, for example:

    [membership-level levelid="2" message="You have no access!"]
    This content should be hidden to membership levels other than 2.
    [/membership-level]

    You can specify the default message in the shortcode too, where it reads 'No Access!'.

    Cheers,
    David

  • DavidM

    Hi chasporter,

    Haven't heard back from you in a moment on this, but I'll be happy to assist with anything related to this matter. As Membership doesn't provide for shortcode functionality in the specific way you were mentioning, you'll find that shortcode I posted would do take care of that for you.

    It can be pasted as is in your theme's functions.php file or better yet, wrapped in some basic plugin code and placed in your plugins folder.
    http://codex.wordpress.org/Writing_a_Plugin

    Let me know if you have any questions on that! :slight_smile:

    -David

  • tishimself

    Hi,

    I added the plugin in a file called membershiplvl.php. I also check in my db for the Level ID#s.

    Then I added this to the about page for my BP Corporate 1.3.7 site for testing purposes as well as the About page on one of the blogs sites:

    [membership-level levelid="1" message="Msg Level ID1: You have no access!"]
    < p>This content should be hidden to membership levels other than 1.</p>
    [/membership-level]

    [membership-level levelid="2" message="Msg Level ID2: You have no access!"]
    < p>This content should be hidden to membership levels other than 2.</p>
    [/membership-level]

    [membership-level levelid="3" message="Msg Level ID3: You have no access!"]
    < p>This content should be hidden to membership levels other than 3.</p>
    [/membership-level]

    [membership-level levelid="4" message="Msg Level ID4: You have no access!"]
    < p>This content should be hidden to membership levels other than 4.</p>
    [/membership-level]

    [membership-level levelid="5" message="Msg Level ID5: You have no access!"]
    < p>This content should be hidden to membership levels other than 5.</p>
    [/membership-level]

    For my BP About page it works for Members but not Visitors.
    For the site blog About page, no one seems to have access to the content. (FYI, I am not loging into the site blog, just my main BP site.)
    To be clear, it is returning the message listed in each of the above shortcodes when access to the content is denied.

    I will test for registered Free Users soon.

    Just for grins, here is what is in the plugin file....
    <?php
    /*
    Plugin Name: Membership Level Shortcode
    Plugin URI: None
    Description: Adds the membership-level Shortcode.
    Version: 1.0
    Author: L McMahon
    Author URI: None
    License: None
    */

    function membership_level( $atts, $content = null ) {

    extract( shortcode_atts( array(
    'levelid' => '1',
    'message' => 'No Access!',
    ), $atts ) );

    //also available: current_user_on_subscription()
    if ( current_user_on_level($levelid) ) {
    return $content;
    }
    else { return $message;}
    }
    add_shortcode('membership-level', 'membership_level');

    ?>

    Larry

  • sophieyanow

    Is this PHP snippet still functional in the latest version of Membership? I simply want to have two different No Access codes.

    I've placed the snippet in my functions.php file, but when I enter a shortcode like [membership-level levelid="2" message="You have no access!"], it still displays the No Access message that I defined in my Membership Options panel (and if I remove that text, it just doesn't display anything at all.)

    Help?

  • DavidM

    Actually, from what you're describing it sounds like it might just have to do with viewing the site as Admin. If you're viewing the site as Admin, you should see all the shortcodes un-rendered.

    Is that the case?

    If not, and you're viewing the site as a logged in member, it's likely you'd just have to use the Shortcode rule when editing your levels in Membership > Access Levels.

    Have you tried configuring those so that members see those or not based on the shortcode rule?

    -David

  • Mason

    Hiya,

    As we haven't heard back from you we're going to assume the problem was sorted out and mark this ticket as resolved.

    If it wasn't resolved, or you have any more questions related to this thread please feel free to post them below and tick the 'Mark as Not Resolved (re-open)' box below the post area (or else we'll miss it!)

    Otherwise, thanks for using the forums, and for being a member of WPMU DEV, it's a pleasure to help you out and we look forward to being of assistance in the future.

    Thanks