I am using WordPress along with Timber to build a custom theme. I need a page of my website to be password protected. I have changed the settings in the WordPress admin area for this page but the out of the box WordPress functionality does not seem to work with Timber. I searched around a bit and found the following code snippets, but they do not work. What am I doing wrong here?
In my page.php
logic:
$context = Timber::get_context();
$post = new TimberPost();
$context['page'] = $post;
$context['global'] = get_fields('options');
$context['protected'] = post_password_required($post->ID); // here i am trying to check if the current page is password protected and adding the check to the context
$context['password_form'] = get_the_password_form(); // grabbing the function to display the password form and adding it to the context
Timber::render( array(
'pages/page-' . $post->post_name . '.twig',
'pages/page.twig'
), $context );
In my Twig template, I tried:
{% if protected %}
{{ password_form }}
{% else %}
<!-- rest of page template here -->
{% endif %}
This if
check does not seem to work, it always says that the page is not password protected even if it has been specified otherwise in the WP admin. The {{ password_form }}
does display the password form correctly.
I also tried this in page.php
:
$context = Timber::get_context();
$post = new TimberPost();
$context['page'] = $post;
$context['global'] = get_fields('options');
if ( post_password_required($post->ID) ) {
Timber::render('page-password.twig', $context);
} else {
Timber::render(array('pages/page-' . $post->post_name . '.twig', 'pages/page.twig' ), $context);
}
With page-password.twig
displaying the password form. This if check also did not work.