PPWP Password Protect Pages Shortcode

Below, you’ll find a detailed guide on how to add the PPWP – Password Protect Pages Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the PPWP – Password Protect Pages Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the PPWP – Password Protect Pages Plugin and the shortcodes it provides:

Plugin Icon
PPWP – Password Protect Pages

"PPWP – Password Protect Pages is a robust WordPress plugin offering advanced security. It allows users to shield their valuable content by setting up unique passwords for individual pages."

★★★★☆ (250) Active Installs: 30000+ Tested with: 6.2.3 PHP Version: 5.6
Included Shortcodes:
  • [PPW]

PPWP Password Protect Pages [PPW] Shortcode

The Password Protect WordPress (PPW) shortcode is a tool used to restrict access to specific page content. It requires users to enter a password to view the content. The shortcode checks for whitelisted roles, content unlock time, and valid passwords. If these conditions are met, the content is displayed. If not, a password form or custom text appears.

Shortcode: [PPW]

Parameters

Here is a list of all possible PPW shortcode parameters and attributes:

  • whitelisted_roles – Defines user roles that can access protected content without a password
  • passwords – Sets specific passwords which can unlock the protected content

Examples and Usage

Basic example – A simple usage of the password protect page shortcode. This example only includes the ‘passwords’ attribute where you can input one or multiple passwords separated by spaces.

[ppw id="1" passwords="123 456 789" /]

Advanced examples

Using the shortcode to protect a page with specific user roles. The ‘whitelisted_roles’ attribute allows you to specify the user roles that can bypass the password restriction. In this example, the roles ‘administrator’ and ‘editor’ are whitelisted.

[ppw id="1" passwords="123 456 789" whitelisted_roles="administrator editor" /]

Using the shortcode to display a password protected page with a custom form. The ‘custom_form’ attribute allows you to customize the form that is displayed when a user tries to access the protected page. This example shows a custom form with a personalized message.

[ppw id="1" passwords="123 456 789" custom_form="Please enter the password to access this page." /]

Using the shortcode to unlock the content at a specific date and time. The ‘unlock_time’ attribute allows you to specify a datetime in ‘Y-m-d H:i:s’ format to automatically unlock the content. This example shows a page that will be unlocked on January 1, 2023, at 12:00 AM.

[ppw id="1" passwords="123 456 789" unlock_time="2023-01-01 00:00:00" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [PPW] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( PPW_Constants::PPW_HOOK_SHORT_CODE_NAME, array( $this, 'render_shortcode' ) );

Shortcode PHP function:

function render_shortcode( $attrs, $content = null ) {
			global $page;

			// In case the shortcode is outside in the loop, the page is 0.
			$number = ! empty( $page ) ? $page : 1;

			$this->attributes = apply_filters( 'ppw_pcp_attributes', $this->attributes, $number );
			$attrs            = shortcode_atts(
				$this->attributes,
				$attrs
			);

			$message = $this->is_valid_shortcode( $attrs, $content );
			$message = apply_filters( 'ppw_pcp_valid_shortcode', $message, $attrs );
			if ( true !== $message ) {
				return $this->get_invalid_shortcode_message( $message, $attrs );
			}

			$content = sprintf(
				'<div class="%s">%s</div>',
				$this->get_main_class_name( $attrs ),
				do_shortcode( $content )
			);

			$whitelisted_roles = apply_filters( PPW_Constants::HOOK_SHORT_CODE_WHITELISTED_ROLES, $attrs['whitelisted_roles'] );

			if ( $this->is_whitelisted_role( $whitelisted_roles ) ) {
				// Remember to wrap the content between the parent div. If you want to replace the shortcode content.
				return apply_filters( PPW_Constants::HOOK_SHORTCODE_RENDER_CONTENT, $content, $attrs );
			}

			// Unlock content by datetime.
			$unlocked = apply_filters( 'ppw_shortcode_unlock_content', $this->is_unlock_content_by_time( $attrs ), $attrs );
			if ( $unlocked ) {
				return apply_filters( PPW_Constants::HOOK_SHORTCODE_RENDER_CONTENT, $content, $attrs );
			}

			do_action( PPW_Constants::HOOK_SHORT_CODE_BEFORE_CHECK_PASSWORD, $content );

			// Passwords attribute format: passwords="123 345 898942".
			$passwords = apply_filters( PPW_Constants::HOOK_SHORTCODE_PASSWORDS, array_filter( explode( ' ', trim( $attrs['passwords'] ) ), 'strlen' ), $attrs );

			foreach ( $passwords as $password ) {
				// When passwords attribute having special characters eg: <script>alert('hello')</script>. WP will encode the HTML tag. Need to decode to compare the value in Cookie.
				$hashed_password = wp_hash_password( wp_specialchars_decode( $password ) );
				if ( $this->is_valid_password( $hashed_password ) ) {
					// Remember to wrap the content between the parent div. If you want to replace the shortcode content.
					return apply_filters( PPW_Constants::HOOK_SHORTCODE_RENDER_CONTENT, $content, $attrs );
				}
			}

			do_action( PPW_Constants::HOOK_SHORT_CODE_AFTER_CHECK_PASSWORD, $content );

			$this->add_scripts();

			// Show custom text instead of password form.
			$custom_form = apply_filters( PPW_Constants::HOOK_SHORTCODE_BEFORE_RENDER_PASSWORD_FORM, false, $attrs );
			if ( false !== $custom_form ) {
				return sprintf(
					'<div class="%s">%s</div>',
					$this->get_main_class_name( $attrs ),
					$this->massage_attributes( $custom_form )
				);
			}

			$password_form = $this->get_restricted_content_form( $attrs, $number );

			return apply_filters( 'ppw_pcp_password_form', $password_form, $attrs );
		}

Code file location:

password-protect-page/password-protect-page/includes/services/class-ppw-shortcode.php

Conclusion

Now that you’ve learned how to embed the PPWP – Password Protect Pages Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *