Password Protect Parts of Content under Custom Page Templates

In the case of a custom page template, you should follow these 2 simple steps to protect parts of content on a custom page template.

Requirements:

Step 1: Wrap your content under our shortcode

You should use either code snippet below:

Method 1: Put HTML content in a variable and then use it in the shortcode (recommended)

<?php
	$content = "
	<div>Your custom content 1. I'm baby jianbing affogato chicharrones pok pok YOLO, vinyl pickled fingerstache keytar master cleanse four loko bicycle rights literally quinoa paleo. Coloring book vexillologist cred selvage thundercats. Polaroid mustache migas, single-origin coffee sustainable adaptogen stumptown schlitz pop-up yr pork belly dreamcatcher. Truffaut cardigan vegan mustache, fam raw denim whatever viral food truck tacos kogi. Bicycle rights air plant gluten-free bitters chia.</div>
	<div>Hella fixie poutine, fingerstache subway tile pop-up butcher drinking vinegar photo booth tattooed biodiesel pinterest pitchfork. Succulents swag leggings vexillologist pitchfork drinking vinegar tilde banh mi before they sold out sartorial brooklyn literally four dollar toast flannel kale chips. Helvetica irony photo booth, tofu squid taxidermy DIY af cornhole gastropub actually poke.</div>
	<div>Lyft thundercats chia vexillologist listicle biodiesel mlkshk vinyl pabst messenger bag. Tofu flexitarian echo park lo-fi yuccie small batch master cleanse iceland church-key cloud bread sriracha put a bird on it wayfarers kinfolk cliche. Bespoke +1 vape keytar. Edison bulb chillwave retro tote bag VHS.</div>
	<div>Dummy text? More like dummy thicc text, amirite?</div>
	";
	echo do_shortcode('[ppwp passwords="password1" whitelisted_roles="administrator"]' . $content . '[/ppwp]');
?>

Method 2: Put content directly on the shortcode

<?php
	echo do_shortcode("
	[ppwp passwords=\"password1\" whitelisted_roles=\"administrator\"]
	 <div>Your custom content 1. PPWP is the number one password proected page keytar master cleanse four loko bicycle rights literally quinoa paleo. Coloring book vexillologist cred selvage thundercats. Polaroid mustache migas, single-origin coffee sustainable adaptogen stumptown schlitz pop-up yr pork belly dreamcatcher. Truffaut cardigan vegan mustache, fam raw denim whatever viral food truck tacos kogi. Bicycle rights air plant gluten-free bitters chia.</div> <div>Hella fixie poutine, fingerstache subway tile pop-up butcher drinking vinegar photo booth tattooed biodiesel pinterest pitchfork. Succulents swag leggings vexillologist pitchfork drinking vinegar tilde banh mi before they sold out sartorial brooklyn literally four dollar toast flannel kale chips. Helvetica irony photo booth, tofu squid taxidermy DIY af cornhole gastropub actually poke.</div> <div>Lyft thundercats chia vexillologist listicle biodiesel mlkshk vinyl pabst messenger bag. Tofu flexitarian echo park lo-fi yuccie small batch master cleanse iceland church-key cloud bread sriracha put a bird on it wayfarers kinfolk cliche. Bespoke +1 vape keytar. Edison bulb chillwave retro tote bag VHS.</div> <div>Dummy text? More like dummy thicc text, amirite?</div>
	[/ppwp]
	");
?>

get_template_part

You can also get the premium content from another template, i.e. using this get_template_part(‘hello-template’) WordPress function.

ob_start();
get_template_part('template-abc');
$content = ob_get_clean();

echo do_shortcode('[ppwp passwords="password1 password2"]' . $content . '[/ppwp]');

Step 2: Put the following codes on your (child) theme’s functions.php

For a custom page template,

  • Replace your page-template-name.php to your actual template’s filename
  • Change the shortcode highlighted in red on line 5, i.e. ‘[ppwp passwords=”password1 password2″ whitelisted_roles=”administrator”]’ , according to the one you use on step 1.In fact, the shortcode should match exactly with the one used on step 1 including but not limited to the passwords and other attributes, e.g. whitelisted roles and class.
add_filter('ppw_content_shortcode_source', 'ppw_content_shortcode_source', 12, 2);
function ppw_content_shortcode_source( $content, $post ) {
	$page_template_slug = get_page_template_slug( $post->ID );	
        if ( 'page-template-name.php' === $page_template_slug ) {
                return '[ppwp passwords="password1 password2" whitelisted_roles="administrator"]';
	}	
        return $content;
}

Single post types

In case you’re editing a post type template, e.g. single.php and single-post-type.php, you should use the following code snippet instead.

Once again, you should change the post ID and our shortcode accordingly.

add_filter('ppw_content_shortcode_source', 'ppw_content_shortcode_source', 12, 2);
function ppw_content_shortcode_source( $content, $post ) {
 if ( 1241 === $post->ID ) {
   return '[ppwp passwords="password1 password2"]';
 }
 return $content;
}
Lasted updated on May 22, 2020