Title: Shortcode in Template
Last modified: August 30, 2016

---

# Shortcode in Template

 *  Resolved [jaeeun](https://wordpress.org/support/users/jaeeun/)
 * (@jaeeun)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/shortcode-in-template-8/)
 * I’m trying this code in my page.php:
 *     ```
       <?php echo do_shortcode('[content_protector password="password"]');?>
       			<?php the_content();?>
       			<?php echo do_shortcode('[/content_protector]');?>
       ```
   
 * but it doesn’t work. The page just displays [/content_protector]. If I look into
   the html, it outputs a div with the “content-protector-access-form” class, then
   the content, then the closing code as a string.
 * [https://wordpress.org/plugins/content-protector/](https://wordpress.org/plugins/content-protector/)

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Thread Starter [jaeeun](https://wordpress.org/support/users/jaeeun/)
 * (@jaeeun)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/shortcode-in-template-8/#post-6259917)
 * I did more research and now I’m using this code:
 *     ```
       <?php
              $password = get_post_custom_values('password');
       	$protected = the_content();
       	echo do_shortcode("[content_protector password=". "'".$password[0]."']". $protected. "[/content_protector]");
       ?>
       ```
   
 * But this displays the content before the password is entered.
 *  [K. Tough](https://wordpress.org/support/users/kjvtough/)
 * (@kjvtough)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/shortcode-in-template-8/#post-6260023)
 * Hi, jaeeun.
 * Assuming you’re already in the Loop, this should work (note that in this snippet,
   the password is actually the string ‘password’ – you probably want to change 
   that):
 *     ```
       <?php
       echo do_shortcode('[content_protector password="password"]
       		. get_the_content()
       		. '[/content_protector]');
       ?>
       ```
   
 *  Thread Starter [jaeeun](https://wordpress.org/support/users/jaeeun/)
 * (@jaeeun)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/shortcode-in-template-8/#post-6260224)
 * Thanks, tested and it works with content().
 * Now I’m trying to apply to what I actually need to protect, which is an iframe
   that’s displayed through this code
 * `do_action( 'tribe_events_single_event_after_the_meta' )`
 * And this is my code
 *     ```
       <?php
       	$password = get_post_custom_values('password');
   
       	if(!empty($password)){
       	echo do_shortcode("[content_protector password=". "'".$password[0]."']". do_action( 'tribe_events_single_event_after_the_meta' ). "[/content_protector]");
       	} else {
       		do_action( 'tribe_events_single_event_after_the_meta' );
       	}
       ?>
       ```
   
 * but again, it just shows the iframe anyway before entering the password…this 
   might be beyond your plugin support, but I’d really appreciate if you could help…!
 *  [K. Tough](https://wordpress.org/support/users/kjvtough/)
 * (@kjvtough)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/shortcode-in-template-8/#post-6260286)
 * Can you return the results of `do_action( 'tribe_events_single_event_after_the_meta')`
   so that it can be assigned into a variable?
 *  [stueynet](https://wordpress.org/support/users/stueynet/)
 * (@stueynet)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/shortcode-in-template-8/#post-6260310)
 * K. Tough is there any way to get Ajax working when doing the do_shortcode method?
   I am trying to run the plugin on a secondary content field (not the main content).
   When doing it with do_shortcode even within the loop it does not enqueue the 
   ajax scripts. Because of server caching I need this to run via ajax.
 *  [PthPndr](https://wordpress.org/support/users/pthpndr/)
 * (@pthpndr)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/shortcode-in-template-8/#post-6260323)
 * Hey jaeeun,
 * Did you ever get this to work?
 * I happened upon this post because I guess I’m trying to do the same thing as 
   you with tribe.
 *  [getyler3](https://wordpress.org/support/users/getyler3/)
 * (@getyler3)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/shortcode-in-template-8/#post-6260339)
 * K Tough, I have a very similar issue… but I have a bunch of echo $print lines
   in between the shortcode and can’t get the ending shortcode working. How would
   your example work in this scenario?
 *     ```
       //      echo do_shortcode( '[content_protector password="xxxxxxx" cookie_expires="0" identifier="xxx"] Private Info  [/content_protector]' );
       echo do_shortcode( '[content_protector password="xxxxxxx" cookie_expires="0" identifier="xxx"]' );
   
             echo '<h2>' . $print->FirstName.' ' . $print->LastName.' </h2>';
             echo '<table class="profile center">';
             echo '<tr><td>Team </td><td>' . $print->Team.'</td></tr>';
             echo '<tr><td>First Name </td><td>' . $print->FirstName.'</td></tr>';
             echo '<tr><td>Nickname </td><td>' . $print->Nickname.'</td></tr>';
             echo '<tr><td>Middle Name </td><td>' . $print->MiddleName.'</td></tr>';
             echo '<tr><td>Last Name </td><td>' . $print->LastName.'</td></tr>';
       ...
             echo '[/content_protector]';
       //      echo do_shortcode( '[/content_protector]' );
       ```
   
 *  [K. Tough](https://wordpress.org/support/users/kjvtough/)
 * (@kjvtough)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/shortcode-in-template-8/#post-6260340)
 * Easiest way would be to put your content into a variable, then run it through`
   do_shortcode()` (it requires the complete shortcode, including opening and closing
   tags). Something like this:
 *     ```
       $the_content = '<h2>' . $print->FirstName.' ' . $print->LastName.' </h2>';
       $the_content .= '<table class="profile center">';
       $the_content .= '<tr><td>Team </td><td>' . $print->Team.'</td></tr>';
       $the_content .= '<tr><td>First Name </td><td>' . $print->FirstName.'</td></tr>';
       $the_content .= '<tr><td>Nickname </td><td>' . $print->Nickname.'</td></tr>';
       $the_content .= '<tr><td>Middle Name </td><td>' . $print->MiddleName.'</td></tr>';
       $the_content .= '<tr><td>Last Name </td><td>' . $print->LastName.'</td></tr>';
       ...
       $the_content .= '/<table>';
       echo do_shortcode( '[content_protector password="xxxxxxx" cookie_expires="0" identifier="xxx"]' . $the_content . '[/content_protector]' );
       ```
   
 *  [getyler3](https://wordpress.org/support/users/getyler3/)
 * (@getyler3)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/shortcode-in-template-8/#post-6260341)
 * Awesome! Works like a charm. Was just looking at [https://wordpress.org/support/topic/do_shortcode-in-theme-template?replies=9](https://wordpress.org/support/topic/do_shortcode-in-theme-template?replies=9)
   trying to apply it to my code. Many thanks!! You rock!

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Shortcode in Template’ is closed to new replies.

 * ![](https://ps.w.org/content-protector/assets/icon-256x256.png?rev=2206760)
 * [Passster - Password Protect Pages and Content](https://wordpress.org/plugins/content-protector/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/content-protector/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/content-protector/)
 * [Active Topics](https://wordpress.org/support/plugin/content-protector/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/content-protector/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/content-protector/reviews/)

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [template](https://wordpress.org/support/topic-tag/template/)

 * 9 replies
 * 5 participants
 * Last reply from: [getyler3](https://wordpress.org/support/users/getyler3/)
 * Last activity: [10 years, 4 months ago](https://wordpress.org/support/topic/shortcode-in-template-8/#post-6260341)
 * Status: resolved