Restrict Content in Theme Programatically by Membership Level

I would like to have a page section that is limited only to a specific Membership Level that is defined in the Theme file because this content is built dynamically and is not part of the_content which offers additional download links to paid customers based on post meta-data.

Would this be accomplished by calling do_shortcode or is there a theme function available?

Also, how would this be affected by caching with W3TC?

  • Patrick

    Hi there @Cagle Webmaster

    I hope you're having a great day!

    Yes indeed. You can use the do_shortcode function with access level shortcodes in your templates to restrict visibility to any content or functions you need to. For example:

    <php echo do_shortcode ('
    [level-visitor] Content for not-logged-in visitors only [/level-visitor]
    [level-level-1] Content for Level 1 users only [/level-level-1]
    '); ?>
  • Ash

    Hello @Cagle Webmaster

    I hope you are well today.

    Yeah you can use the template function: current_user_on_level which needs the level id to be passed, like:

    <?php
    if( current_user_on_level( 1 ) ){
    ?>
    CONTENT for level 1 user
    <?php
    }elseif( current_user_on_level( 2 ) ){
    ?>
    CONTENT for level 2 user
    <?php
    }elseif( current_user_on_level( 3 ) ){
    ?>
    CONTENT for level 3 user
    <?php
    }
    
    ... so on

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

  • Cagle Webmaster

    Ash,

    Correct. If I use the shortcode programatically and there is no shortcode in the post, it does not load the CSS. I found a way around this by simply putting the shortcode in the post content and hiding the display of the button via CSS to force Membership to output the stylesheet. Then using the shortcode elsewhere in the theme template, outside of the loop works just find.

    I'll attach the two files I've implemented for this theme but you can see it in action at http://www.cagle.com/funding

    Membership.php has the method calls and page-funding.php is the actual theme file.

  • Cagle Webmaster

    Sorry, I didn't realize you blocked php and txt file uploads in the support forum. I'll paste the content below instead.

    mytheme/framework/membership.php:

    <?php
    ////////////////////////////////////////////////////////////////////////////////
    // Customization options and functions related to the WPMUDEV Membership plugin
    ////////////////////////////////////////////////////////////////////////////////
    /*
    $funding_pledges = 0;
    $funding_status = 0;
    $funding_supporters = 0;
    */
    
    function dcpci_widget_member_promo($level, $more = '') {
    	?>
        	<div id='dcpci-member-promo-<?php echo $level; ?>' class="widget promo rounded">
    
            	<div class="promo-title" id="promo-title-<?php echo $level; ?>">
                	<?php echo do_shortcode('[subscriptiontitle subscription="'.$level.'"]'); ?>
                </div>
    
            	<div class="promo-price" id="promo-price-<?php echo $level; ?>">
                	$<?php echo do_shortcode('[subscriptionprice subscription="'.$level.'"]'); ?>/mth<div class="sub"> or more</div>
                </div>
    
                <div class="promo-details" id="promo-details-<?php echo $level; ?>">
                	<?php echo do_shortcode('[subscriptiondetails subscription="'.$level.'"]'); ?>
                </div>
    
                <div class="promo-options" id="promo-options-<?php echo $level; ?>">
                    <div class="promo-button" id="promo-button-<?php echo $level; ?>">
                        <?php echo do_shortcode('[subscriptionbutton subscription="'.$level.'"]Support Us[/subscriptionbutton]'); ?>
                    </div>
    
                    <?php if (!empty($more)) { ?>
                    <div class="promo-more" id="promo-more-<?php echo $level; ?>">
                        <a href="<?php echo $more ?>" class="promo-more-link button blue">Details</a>
                    </div>
                    <?php } ?>
    			</div>
            </div>
    
        <?php
    }
    
    function dcpci_subscription_form() {
    	echo do_shortcode('[subscriptionform]');
    }
    
    function dcpci_funding_buttons() {
    ?>
    	<div id="dcpci-funding-options" >
        	<div id="dcpci-fo-header">
            	<h2>Become A Supporter!</h2>
            </div>
        	<div id="dcpci-fo-supporters" class="rounded">
    
                <div class="dcpci-fo-buttons">
    
                    <div class="dcpci-fo-title">
                        For Supporters:
                    </div>
    
                	<?php echo do_shortcode('[subscriptionbutton subscription="1"]$1[/subscriptionbutton]'); ?>
                    <?php echo do_shortcode('[subscriptionbutton subscription="2"]$3[/subscriptionbutton]'); ?>
                    <?php echo do_shortcode('[subscriptionbutton subscription="3"]$6[/subscriptionbutton]'); ?>
                    <?php echo do_shortcode('[subscriptionbutton subscription="4"]$15[/subscriptionbutton]'); ?>
                    <?php echo do_shortcode('[subscriptionbutton subscription="5"]$25[/subscriptionbutton]'); ?>
    
                </div>
            </div>
            <div id="dcpci-fo-publishers" class="rounded">
    
                <div class="dcpci-fo-buttons">
    
                    <div class="dcpci-fo-title">
                        For Publishers
                    </div>
    
                	<?php echo do_shortcode('[subscriptionbutton subscription="6"]$50[/subscriptionbutton]'); ?>
                    <?php echo do_shortcode('[subscriptionbutton subscription="7"]$100[/subscriptionbutton]'); ?>
                    <?php echo do_shortcode('[subscriptionbutton subscription="8"]$300[/subscriptionbutton]'); ?>
                    <?php echo do_shortcode('[subscriptionbutton subscription="9"]$1000[/subscriptionbutton]'); ?>
                    <?php echo do_shortcode('[subscriptionbutton subscription="11"]$2000[/subscriptionbutton]'); ?>
    
                </div>
            </div>
    
        </div>
    
    <?php	
    
    }
    
    ?>

    page-funding.php - theme file implementing the membership shortcodes and registration buttons:

    <?php
    /*
    Template Name: Funding Promo
    */
    ?>
    <!DOCTYPE html>
    <html 	xmlns="http://www.w3.org/1999/xhtml"
    		xmlns:og="http://ogp.me/ns#"
    		xmlns:fb="http://opg.me/ns/fb#>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Cagle.com Supporter Funding</title>
    
        <meta name="keywords" content="political cartoon, poliitcal meme, comic, cartoon, politics, editorial cartoons, humor, political humor, web comic, webcomic" />
    
        <style>
    
    		body, #page, #header, #footer, #sidebar	{background-color: #394973; font-family: 'Futura LT Book', Tahoma, Verdana; font-size: 25px;}
    		#page-body 	{background-color: white; width: 950px; margin: auto; }
            #editor {float: right; display: block; font-size: 10px;}
    		#header 	{ padding-top: 20px; padding-bottom: 20px; margin: auto;}
    		#post 		{background-color: white; padding: 20px; width: 600px; float: left; }
    		#title 		{font-size: 35px; padding: 10px; padding-left: 0px;}
    		#content 	{  }
    		#sidebar 	{ float: right; width: 275px; padding-left: 10px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-right: 20px; padding-bottom: 20px;}
    		#sidebar h3 {color: #fff; font-weight: bold; text-transform: uppercase; text-align: center;}
    
    		.widget {background-color: white; padding: 10px; margin-bottom: 20px;}
            .promo-details {font-size: 12px;}
            .promo-options {min-height: 45px; }
    		.promo-button {float: right; display: inline-block;}
            .promo-more {float: left; display: block;}
    		.alignright {float: right;}
            .sub {display: inline; font-size: .7em;} 
    
            #dcpci-fo-supporters, #dcpci-fo-publishers {margin: 0px; padding: 0px; text-align: center; width: 270px; border: solid 2px #0076a3; }
            #dcpci-fo-supporters {padding: 10px; float: left;  background-color: #FFE871;}
            #dcpci-fo-publishers {padding: 10px; float: right; background-color: #F2d53e;}
            .dcpci-fo-title {padding-bottom: 5px;}
    
            .button.blue {font-size: .75em !important; margin-bottom: 5px !important;}
            .rounded {border-radius: .5em;}
    
    	</style>
    
    	 <?php wp_head(); ?>
         <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/googleanalytics.js"></script>
    
    </head>
    
    <body id="dcpi-page-funding">
        <div id="page">
            <div id="page-body" >
    
                <div id="header">
                	<a href="/funding/"><img src="<?php bloginfo('template_directory'); ?>/images/header/caglepost_title.png"></a>
                </div>
    
                 <?php if(have_posts()) : ?>
                    <?php while(have_posts()) : the_post(); ?>
    
                <div id="post"  class="rounded">
    
                	<div id="editor">
                    	<?php edit_post_link(); ?>
                    </div>
    
                    <div id="title" >
                    	<?php the_title(); ?>
                    </div>
    
                    <div id="content">
                        <?php the_content(); ?>
                    </div>
    
                    <?php dcpci_funding_buttons(); ?>
    
                </div>
    
                 <div id="sidebar">
    
               		<h3>Ways you can support us!</h3>
    
                    <?php dcpci_widget_member_promo(1, '/funding/bronze-supporter/'); ?>
                    <?php dcpci_widget_member_promo(2, '/funding/silver-supporter/'); ?>
                    <?php dcpci_widget_member_promo(3, '/funding/gold-supporter/'); ?>
                    <?php dcpci_widget_member_promo(4, '/funding/platinum-supporter/'); ?><?php dcpci_widget_member_promo(5, '/funding/diamond-supporter/'); ?>
    
                    <h3>Are You A Publisher?</h3>
    
                    <?php dcpci_widget_member_promo(6, '/funding/bronze-publisher/'); ?>
                    <?php dcpci_widget_member_promo(7, '/funding/silver-publisher/'); ?>
                    <?php dcpci_widget_member_promo(8, '/funding/gold-publisher/'); ?>
                    <?php dcpci_widget_member_promo(9, '/funding/platinum-publisher/'); ?>
                    <?php dcpci_widget_member_promo(11, '/funding/diamond-publisher/'); ?>
    
                 </div>
    
                    <?php endwhile; ?>
                    <?php else : ?>
                        <h2>The page had been deleted</h2>
                    <?php endif; ?>
    
                <div id="footer">
    
                </div>
    
            </div>
        </div>    
    
    <?php wp_footer(); ?>
    </body>
    </html>
  • Cagle Webmaster

    Here is a link where I use the shortcode via do_shortcode but do not include the shortcode in the post content. I also commented out the override for the style setting of the buttons, so all these settings are now using the default styles.

    http://www.cagle.com/funding/membership-test/

    As you can see, the stylesheet did not load.

    The following link uses the do_shortcode in theme (same as above) but also includes a regular shortcode call in the post content:

    http://www.cagle.com/funding/

  • Rheinard

    Hi @Cagle Webmaster,

    I can see what you're trying to do here. The reason the styles are not getting applied is because they never get loaded.

    Membership uses the 'the_post' hook to determine if a particular shortcode is present and then execute associate code. This means that it only checks for post content and if the shortcodes are called anywhere else (i.e. theme files) then the styles never get applied.

    The fact that they are getting applied though for the shortcodes means that there is still some legacy code that needs to be removed. Ideally your theme's stylesheet should be responsible for the buttons, not the Membership plugin. So those blue buttons that get generated really should not be in the first place.

    If you do like the style however, you can look at buttons.css in the 'css' folder and copy it over into your theme.

    Cheers,
    Rheinard

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.