Patreon WordPress - how to lock any part of your Website

How to lock any section, page, type of content, or template section through the code


Its possible to lock any section of the site, a widget, some content in widget, some piece of meta value shown in the header, footer, or even remote embedded content displayed somewhere by using custom code blocks and developer toggles.

First, it is necessary to turn on the direct unlock mechanism by setting the below global variable somewhere. This may be in your theme’s functions.php, and better yet, a custom small plugin added and toggled with just the below code:

<?php

/*
Plugin Name: Patreon Dev Plugin
Plugin URI: 
Description: Plugin to hold any customizations, hooks, filters, settings for development
Version: 1.0.0
Author: Patreon <platform@patreon.com>
Author URI: https://patreon.com
*/

$GLOBALS['patreon_enable_direct_unlocks'] = true;

This global will allow the plugin to skip security checks that prevent sending users to Patreon without supplying post ids, so you can create and use the unlock interface anywhere without having to lock a post.

Afterwards, the below code can be used anywhere to lock/show content. The below code checks for patronage level, and if it is the amount you set in the function parameters, shows the content. This code can be put into header, or, for example in the theme element that shows webcomics in headers or widgets as meta values.

 <?php

                // Custom lock
                // Get current user if it is not already present
                if ( !$user ) {
                        $user = wp_get_current_user();
                }
                // Check how much patronage the user has
                $user_patronage = Patreon_Wordpress::getUserPatronage();
                // Check if user is a patron whose payment is declined
                $declined = Patreon_Wordpress::checkDeclinedPatronage($user);
                
                // Set where we want to have the user land at - this can be anything. The below code sets it to this exact page. This page may be a category page, search page, a custom page url with custom parameters - anything
                
                $redir = home_url(add_query_arg($_GET,$wp->request));
                
                // If the user has less patronage than, say, $7, or declined, and is not an admin-level user, lock the post.

                if ( ( $user_patronage < 700 
                                OR $declined )
                                AND !current_user_can( 'manage_options' )
                ) {
                                // Generate the unlock interface wherever it is
                     echo Patreon_Frontend::displayPatreonCampaignBanner(
                                        7, 
                                        array( 'direct_unlock' => 7,