1
votes

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.

3

3 Answers

0
votes

You can use this code to contain the content that should be password protected:

if ( ! post_password_required() ) {

    // Include password protected content here    

}
0
votes

By default timber output a form to enter the password.

{{page.content}}

But if you want to know if the page has a password you can do

$context = Timber::get_context();
$post = Timber::query_post();
$context['post'] = $post;

if ( post_password_required( $post->ID ) ) {
    Timber::render( 'single-password.twig', $context );
} else {
    Timber::render( array( 'single-' . $post->ID . '.twig', 'single-' . $post->post_type . '.twig', 'single.twig' ), $context );
}

Also try to see the page on a private window, because if you are login as admin you will see as a normal page.

0
votes

Your page.php looks good. That part should work. But for your Twig template, you need to change the syntax for your if-else-tags. You’ll want to use Control Structure tags, so you need to use {% %} instead of echoing tags {{ }}.

{% if protected %}
    {{ password_form }}
{% else %}
    {# Rest of the template here #}
{% endif %}

Update

In the meantime, this was discussed as a Timber issue and the new recommended way of adding password protection is to use a filter: https://timber.github.io/docs/guides/wp-integration/#using-a-filter.