How to Password Protect Divi Theme Builder Template

By default, our Password Protect WordPress (PPWP) Pro only protects Divi’s post content module. As a result, when you build a page template using Theme Builder without this module, its content will be shown even though you have enabled password protection.

PPWP Pro: Divi theme builder

Once you have everything set up with Divi Theme Builder, simply add the following code snippet to your (child) theme’s functions.php file.

add_action( 'et_theme_builder_after_layout_opening_wrappers', 'ppwp_divi_handle_after_layout_opening_wrappers', 10, 2 );
function ppwp_divi_handle_after_layout_opening_wrappers( $layout_type, $layout_id ) {
	if ( ! function_exists( 'ppw_core_render_login_form' ) ) {
		return;
	}

	add_filter( 'et_builder_render_layout',
		function ( $content ) use ( $layout_type ) {
			$post_id = call_user_func( 'et_core_page_resource_get_the_ID' );
			if ( ! is_singular() ) {
				return $content;
			}

			if ( post_password_required( $post_id ) ) {
				if ( 'et_header_layout' === $layout_type || 'et_footer_layout' === $layout_type ) {
					return '';
				}
				global $post;
				$old_post_id = $post->ID;
				$post->ID    = $post_id;
				$form           = ppw_core_render_login_form();
				$post->ID    = $old_post_id;

				return $form;
			}

			return $content;
		}
	);
}

This code only applies to Individual content protection. If you select an Archive page on your Divi template, for example, this code won’t be working. You will need to use our Area Content Protection instead.

Lasted updated on September 21, 2021