Introduction
Building custom WordPress themes often requires a level of control beyond what shortcodes or page builder widgets can offer. When it comes to implementing gated content – resources restricted behind an access request or token – developers need programmatic access to the underlying logic. This is where WordPress template tags become invaluable. They empower you to integrate content gating seamlessly into your custom designs, ensuring a consistent user experience and complete design freedom.
This reference guide will delve into how you can use WordPress template tags and direct PHP functions to manage and display gated content within your custom theme, specifically leveraging the robust capabilities of a plugin like WordPress Gatekeeper Pro. We will cover checking access, rendering forms, displaying secure resources, and applying custom styling with PHP and CSS.
Understanding Gated Content in WordPress Themes
Gated content refers to any digital asset – be it a document, video, or a specific page – that requires a visitor to perform an action, such as submitting a form and receiving an approved access token, before gaining access. For WordPress developers, integrating this functionality directly into a custom theme offers several advantages:
- Complete Design Control: Rather than relying on a plugin's default output, you can dictate every pixel of the user interface, ensuring it aligns perfectly with your brand and theme design.
- Enhanced User Experience: Tailor the access request workflow and content display to be intuitive and engaging for your specific audience.
- Seamless Integration: Embed gating logic directly into your theme's templates, making it a native part of the site's structure rather rather than an overlaid plugin component.
- Custom Logic: Implement specific conditions or complex display rules that might not be possible with out-of-the-box shortcodes.
While shortcodes are convenient for content editors, theme developers require a more direct, programmatic interface. Template tags provide this interface, allowing you to build dynamic, data-driven layouts for restricted content.
The Role of Template Tags for Content Gating
In WordPress, template tags are PHP functions used within your theme files to retrieve and display data from the database, perform checks, or render dynamic elements. For gated content, these tags allow you to:
- Determine Lock Status: Check if a particular post or custom post type is designated as "locked."
- Verify User Access: See if the current visitor has an approved token for the locked content.
- Render Dynamic Elements: Output an access request form, a secure download link, or an embedded video player based on the access status.
- Apply Conditional Logic: Display different content or styling based on whether content is gated, accessible, or pending approval.
Gatekeeper Pro provides a suite of template tags, all consistently prefixed with gatekeeper_pro_, and direct class methods specifically designed to give theme developers granular control over every aspect of gated content. This allows for deeply integrated and highly customised restricted content experiences, moving beyond the standard shortcode approach that can sometimes feel restrictive in a custom theme context.
Core Gatekeeper Pro Template Tags and Functions
Gatekeeper Pro offers a range of PHP functions (template tags) and direct class methods that you can use within your theme's .php files. All template tags use the consistent gatekeeper_pro_ prefix. These functions are your primary tools for building custom gated content experiences.
Checking Content and Access Status
These functions are fundamental for applying conditional logic in your templates.
gatekeeper_pro_is_locked( int $post_id = 0 ): boolThis function checks if a given post (or the current post if
$post_idis omitted) has been marked as "locked" by Gatekeeper Pro. It returnstrueif the content is locked,falseotherwise.<?php if ( gatekeeper_pro_is_locked() ) { // This content is locked. } else { // This content is not locked. } ?>gatekeeper_pro_has_access( int $post_id = 0 ): boolDetermines if the current visitor or session has an active, valid token to access the specified locked content. This is crucial for deciding whether to show the content or the request form.
<?php if ( gatekeeper_pro_is_locked() && ! gatekeeper_pro_has_access() ) { // Content is locked, and user does not have access. // Show request form or locked message. } else { // Content is either not locked, or user has access. // Display the actual content. } ?>
Rendering Gating Elements
These functions allow you to output the necessary HTML for the access request form, secure downloads, or video players, or retrieve secure URLs for custom integrations.
gatekeeper_pro_display_request_form( int $post_id = 0, array $args = array() ): voidOutputs the AJAX-powered access request form directly into your template. You can pass an array of arguments to customise its behaviour or appearance, though most styling is handled via CSS. This is essential for
wordpress php content gatingwhere you need a custom form placement.<div class="custom-request-wrapper"> <?php gatekeeper_pro_display_request_form(); ?> </div>gatekeeper_pro_get_download_url( int $post_id = 0 ): string|falseReturns the secure, token-validated URL for a protected file resource if the user has access. This URL links to Gatekeeper Pro's proxy endpoint, which validates the token before serving the file. This ensures that even if a user has access, they cannot share a direct file URL.
<?php if ( gatekeeper_pro_has_access() ) { $download_url = gatekeeper_pro_get_download_url( get_the_ID() ); if ( $download_url ) { echo '<a href="'. esc_url( $download_url ) .'" class="button">Download Resource</a>'; } } ?>gatekeeper_pro_get_video_url( int $post_id = 0 ): string|falseReturns the secure, token-validated URL for a protected video stream if the user has access. Gatekeeper Pro proxies video streams to protect them. This URL can be used with custom video players.
<?php if ( gatekeeper_pro_has_access() ) { $video_url = gatekeeper_pro_get_video_url( get_the_ID() ); if ( $video_url ) { echo '<video controls src="'. esc_url( $video_url ) .'">Your browser does not support the video tag.</video>'; } } ?>gatekeeper_pro_display_content_gate( int $post_id = 0, array $args = array() ): voidThis powerful template tag outputs the entire content gate for a post, dynamically displaying either the locked message, the access request form, or the actual content (including secure files/videos) based on the user's access status. It acts as a wrapper for all gating logic.
<?php // Displays the full gated content experience based on access status. gatekeeper_pro_display_content_gate(); ?>
Advanced Class Methods for Direct Access
For highly advanced integrations, custom logic, or when developing extensions for Gatekeeper Pro, developers can directly interact with the plugin's core components via their respective class instances. These provide granular control over specific functionalities:
Token_Manager:Manages the creation, validation, and expiry of access tokens.Field_Detector:Handles the auto-detection and mapping of content fields (e.g., locked status, resource files).File_Handler:Manages secure file storage, randomised filenames, and .htaccess protection.Proxy_Endpoint:Controls the secure streaming of protected files and videos through a validated proxy.
These components are typically accessed through the main plugin instance, for example: Gatekeeper_Pro::get_instance()->token_manager->validate_token($token_string);
Practical Implementation Examples
Let's explore how to integrate these template tags into your custom theme files for various gated content scenarios. This is key for wordpress custom theme restricted content.
Example 1: Conditional Content Display for a Single Post
Imagine you have a single post template (e.g., single.php or single-custom-post-type.php) where you want to show the full content only if the user has access. Otherwise, a request form is displayed.
<?php get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<?php
while ( have_posts() ) : the_post();
if ( gatekeeper_pro_is_locked( get_the_ID() ) && ! gatekeeper_pro_has_access( get_the_ID() ) ) {
// Content is locked, and user does not have access.
// Display a custom locked message and the request form.
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'gated-content-wrapper' ); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p>This content is exclusive. Please request access to view.</p>
<?php gatekeeper_pro_display_request_form( get_the_ID() ); ?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
<?php
} else {
// Content is either not locked, or user has access. Display full content.
get_template_part( 'template-parts/content', get_post_type() );
}
endwhile; // End of the loop.
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>

