How to Password Protect Whole Divi Template

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

To protect the whole Divi template, no matter if it contains the post content module, 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;
		}
	);
}
Lasted updated on December 22, 2020