{"action":"created","issue":{"url":"https://api.github.com/repos/timber/timber/issues/1800","repository_url":"https://api.github.com/repos/timber/timber","labels_url":"https://api.github.com/repos/timber/timber/issues/1800/labels{/name}","comments_url":"https://api.github.com/repos/timber/timber/issues/1800/comments","events_url":"https://api.github.com/repos/timber/timber/issues/1800/events","html_url":"https://github.com/timber/timber/issues/1800","id":363742424,"node_id":"MDU6SXNzdWUzNjM3NDI0MjQ=","number":1800,"title":"Support/Bug: Password protected page functionality","user":{"login":"erbanach","id":11987517,"node_id":"MDQ6VXNlcjExOTg3NTE3","avatar_url":"https://avatars2.githubusercontent.com/u/11987517?v=4","gravatar_id":"","url":"https://api.github.com/users/erbanach","html_url":"https://github.com/erbanach","followers_url":"https://api.github.com/users/erbanach/followers","following_url":"https://api.github.com/users/erbanach/following{/other_user}","gists_url":"https://api.github.com/users/erbanach/gists{/gist_id}","starred_url":"https://api.github.com/users/erbanach/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/erbanach/subscriptions","organizations_url":"https://api.github.com/users/erbanach/orgs","repos_url":"https://api.github.com/users/erbanach/repos","events_url":"https://api.github.com/users/erbanach/events{/privacy}","received_events_url":"https://api.github.com/users/erbanach/received_events","type":"User","site_admin":false},"labels":[{"id":98068066,"node_id":"MDU6TGFiZWw5ODA2ODA2Ng==","url":"https://api.github.com/repos/timber/timber/labels/doc-request","name":"doc-request","color":"c7def8","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2018-09-25T19:58:39Z","updated_at":"2018-10-01T07:11:46Z","closed_at":null,"author_association":"NONE","body":"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?\r\n\r\nIn my `page.php` logic:\r\n\r\n```\r\n    $context = Timber::get_context();\r\n    $post = new TimberPost();\r\n    $context['page'] = $post;\r\n    $context['global'] = get_fields('options');\r\n    $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\r\n    $context['password_form'] = get_the_password_form(); // grabbing the function to display the password form and adding it to the context\r\n    Timber::render( array(\r\n    'pages/page-' . $post->post_name . '.twig',\r\n    'pages/page.twig'\r\n    ), $context );\r\n```\r\n\r\nIn my Twig template, I tried:\r\n\r\n```\r\n    {% if protected %}\r\n    {{ password_form }}\r\n    {% else %}\r\n    <!-- rest of page template here -->\r\n    {% endif %}\r\n```\r\n\r\nThis `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. How can I get this working?\r\n\r\nI also tried this in `page.php`:\r\n\r\n```\r\n    $context = Timber::get_context();\r\n    $post = new TimberPost();\r\n    $context['page'] = $post;\r\n    $context['global'] = get_fields('options');\r\n\r\n    if ( post_password_required($post->ID) ) {\r\n      Timber::render('page-password.twig', $context);\r\n      } else {\r\n      Timber::render(array('pages/page-' . $post->post_name . '.twig', 'pages/page.twig' ), $context);\r\n      }\r\n```\r\n\r\nWith `page-password.twig` displaying the password form. This if check also did not work. This might be a Timber bug because I believe those functions should be working.\r\n\r\n[Stack overflow question here.](https://stackoverflow.com/questions/52505820/password-protected-page-functionality-not-working-out-of-the-box-with-timber-twi)"},"comment":{"url":"https://api.github.com/repos/timber/timber/issues/comments/425809774","html_url":"https://github.com/timber/timber/issues/1800#issuecomment-425809774","issue_url":"https://api.github.com/repos/timber/timber/issues/1800","id":425809774,"node_id":"MDEyOklzc3VlQ29tbWVudDQyNTgwOTc3NA==","user":{"login":"gchtr","id":2084481,"node_id":"MDQ6VXNlcjIwODQ0ODE=","avatar_url":"https://avatars2.githubusercontent.com/u/2084481?v=4","gravatar_id":"","url":"https://api.github.com/users/gchtr","html_url":"https://github.com/gchtr","followers_url":"https://api.github.com/users/gchtr/followers","following_url":"https://api.github.com/users/gchtr/following{/other_user}","gists_url":"https://api.github.com/users/gchtr/gists{/gist_id}","starred_url":"https://api.github.com/users/gchtr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gchtr/subscriptions","organizations_url":"https://api.github.com/users/gchtr/orgs","repos_url":"https://api.github.com/users/gchtr/repos","events_url":"https://api.github.com/users/gchtr/events{/privacy}","received_events_url":"https://api.github.com/users/gchtr/received_events","type":"User","site_admin":false},"created_at":"2018-10-01T07:11:46Z","updated_at":"2018-10-01T07:11:46Z","author_association":"MEMBER","body":"@tnottu I like you solution very much 😎! This is indeed much cleaner. And even if you wanted to use different templates for different post types, you could do:\r\n\r\n```php\r\n$template = locate_template( [\r\n    'password-protected.php',\r\n    \"password-protected-{$post->post_type}.php\",\r\n] ) ?: $template;\r\n```\r\n\r\nI added a [pull request](https://github.com/timber/timber/pull/1804) that adds your solution to the documentation."}}
{"actor":{"display_login":"gchtr"}}