Using Membership Shortcodes in Template

Is it possible to use the member level shortcodes in my custom template? If so, how?

  • Vinod Dalvi

    Hi @Running,

    I hope you are well today and thank you for your question.

    Yes it is possible to use the member level shortcodes in your custom template using the do_shortcode function like following.

    echo do_shortcode('[levelshortcode]'.$text_to_be_wrapped_in_shortcode.'[/levelshortcode]');

    You will find more information about do_shortcode function on the following page.

    http://codex.wordpress.org/Function_Reference/do_shortcode

    Please advise if you have more questions.

    Kind Regards,
    Vinod Dalvi

  • Running Alpha Admin

    @Vinod, Thank you for the example and reference. I have HTML and PHP content to protect inbetween the shortcodes and must not be inserting it correctly as I get syntax errors. Is there anything special I need to do besides wrap like so?

    echo do_shortcode('[levelshortcode]'.  <div class="row top-spacer">
      <div class="col-md-12"> <p><?php the_content(); ?></p> </div>
      </div> .'[/levelshortcode]');

    Am I doing it wrong?

  • Vinod Dalvi

    Hi @Running,

    Thank you for your quick reply.

    You are using the_content function in your code that echos content instead you should use the function get_the_content as following that returns content and not echo.

    <?php
    echo do_shortcode('[levelshortcode]'.  <div class="row top-spacer">
      <div class="col-md-12"> <p><?php get_the_content(); ?></p> </div>
      </div> .'[/levelshortcode]');

    Best Regards,
    Vinod Dalvi

  • Vinod Dalvi

    Try using the following code.

    <?php
    global $post;
    echo do_shortcode('[levelshortcode]<div class="row top-spacer">
    <div class="col-md-12"> <p>'. $post->post_content . '</p> </div>
    </div>[/levelshortcode]'); ?>

    Also please make sure to change the levelshortcode in the above code according to the membership access level shortcode you have created on the following path as displayed in the attached screenshot.

    Admin Area -> Membership -> Access Levels

  • Running Alpha Admin

    Excellent, that bit of code does work! Thank you! Yes I was inserting my access levels. :wink:

    Now, it gets a bit trickier. I was using the content and surrounding divs as a quick example to get the feel for how the code would look. With the last example you gave, I'm not sure how I would go about surrounding a much larger piece of code (divs, php, etc.) See below. Any tips?

    <div class="row top-spacer">
      <div class="col-md-12">
      <!-- Symbol Table -->
    <div class="table-responsive">
      <table class="table">
     <thead><tr class="info"><th>Equity <span class="text-muted">(Symbol)</span></th><th>Bid Lines</th><th>Offering Lines</th><th>OPS <a href="#" data-toggle="tooltip" data-placement="top" title="Origin of Positive Sentiment"><small><span class="glyphicon glyphicon-info-sign"></span></small></a></th><th>Critical Mass Line</th></tr></thead>
    
     <tbody><tr class="grey"><td><!--Equity/Symbol--></td><td><table width="100%"><tr><td width="33%">1</td><td width="33%">2</td><td width="33%">3</td></tr></table></td><td><table width="100%"><tr><td>1</td><td>2</td><td>3</td></tr></table></td><td><!--OPS--></td><td><table width="100%"><tr><td>1</td><td>2</td><td>3</td></tr></table></td></tr>
    
    	<tr>
        <td>
      <?php the_title(); ?> <span class="text-muted">(<?php the_field('symbol'); ?>)</span>
    </td>
    
        	<td>
            <table width="100%">
                <tr><td width="33%"><p><span class="text-muted">$</span><?php the_field('bid_line_1'); ?></p></td><td width="33%"><p><span class="text-muted">$</span><?php the_field('bid_line_2'); ?></p></td><td width="33%"><p><span class="text-muted">$</span><?php the_field('bid_line_3'); ?></p></td></tr>   </table>
        </td>
    <td>
            <table width="100%">
                 <tr><td width="33%"><p><span class="text-muted">$</span><?php the_field('offering_line_1'); ?></p></td><td width="33%"><p><span class="text-muted">$</span><?php the_field('offering_line_2'); ?></p></td><td width="33%"><p><span class="text-muted">$</span><?php the_field('offering_line_3'); ?></p></td></tr>   </table>
        </td>
        <td><p><span class="text-muted">$</span><?php the_field('origin_of_positive_sentiment'); ?></p></td>
        <td> <table width="100%">
                <tr><td width="33%"><p><span class="text-muted">$</span><?php the_field('critical_mass_line_1'); ?></p></td><td width="33%"><p><span class="text-muted">$</span><?php the_field('critical_mass_line_2'); ?></p></td><td width="33%"><p><span class="text-muted">$</span><?php the_field('critical_mass_line_3'); ?></p></td></tr>   </table></td>
    
    </tr>
    </tbody>
    </table>
    </div>
    <!-- Symbol Table End-->
      </div>
      </div>
    
      <div class="row top-spacer">
      <div class="col-md-12"> <p><?php $content = get_the_content(''); print $content; ?></p> </div>
      </div>
    • Vinod Dalvi

      You can just create a function in the functions.php file of your theme and add that larger piece of code (divs, php, etc.) in the string in that function which will return the string on calling it something like following..

      function get_shortcode_content(){
            $content = ''; // Add the content here
          return $content;
      }

      Now just call this function as a do_shortcode function parameter.

      Note that the parameter of the function do_shortcode should never produce output of any kind. Shortcode functions should return the text. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.

      So in your code if you are using ACF the_field function then change it to ACF get_field function.

      Cheers,
      Vinod Dalvi

  • Jack Kitterhing

    Hi there @Running Alpha Admin

    Hope you're well today, that's correct. :slight_smile:

    So you add your content to function within that function in the string to be returned when it's called, if that makes sense?

    Where you have have $content = ''; // add the content here, in the ' ' would be your content as a string.

    Any issues, we're here to help. :slight_smile:

    Thanks!

    Kind Regards
    Jack.