PHPackages                             jorenvanhee/craft-template-guard - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. jorenvanhee/craft-template-guard

ActiveCraft-plugin[Authentication &amp; Authorization](/categories/authentication)

jorenvanhee/craft-template-guard
================================

Password protect any page or entry in Craft CMS.

4.0.0(1y ago)57.5k↓33.3%2[3 issues](https://github.com/jorenvanhee/craft-template-guard/issues)[1 PRs](https://github.com/jorenvanhee/craft-template-guard/pulls)proprietaryPHPPHP ^8.0.2

Since Jul 5Pushed 1y ago1 watchersCompare

[ Source](https://github.com/jorenvanhee/craft-template-guard)[ Packagist](https://packagist.org/packages/jorenvanhee/craft-template-guard)[ RSS](/packages/jorenvanhee-craft-template-guard/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (9)Versions (17)Used By (0)

Template Guard plugin for Craft CMS 💂
=====================================

[](#template-guard-plugin-for-craft-cms-)

Password protect any page or entry in Craft CMS.

- Protect one or more pages.
- Protect multiple entries each with their own password.
- Customize the login page to match your site's design.
- Easily add log out button.

Quick start
-----------

[](#quick-start)

### Installation

[](#installation)

Install the plugin via the plugin store or install manually using composer.

```
composer require jorenvanhee/craft-template-guard

```

Usage
-----

[](#usage)

This plugin provides a `protect` method in your templates, redirecting users to a login page.

```
{% do craft.templateGuard.protect('password', 'key') %}
```

### Arguments

[](#arguments)

#### Password (string, array)

[](#password-string-array)

The password your users need to provide before accessing the page. You can also provide an array of passwords. The page will not be protected when the password is an empty string or array.

#### Key (string)

[](#key-string)

Identify a protected area on your site using the key argument. When logged in to a page with a specific key, other pages using the same key will also be accessible.

Examples
--------

[](#examples)

### Protect a single page

[](#protect-a-single-page)

Get started quickly by adding the following code to the template you want to protect.

```
{% do craft.templateGuard.protect('Pa$$w0rd', 'secret-page') %}
```

You can also use a password stored on the entry.

```
{% do craft.templateGuard.protect(entry.myPasswordField, 'secret-page') %}
```

Provide multiple passwords: any of the passwords provided in the array can be used to log in.

```
{% do craft.templateGuard.protect(['password1', 'password2'], 'secret-page') %}
```

### Protect multiple pages with the same password

[](#protect-multiple-pages-with-the-same-password)

The second argument (key) identifies a protected area on your site. When logged in to a page with a certain key, other pages using the same key will also be accessible.

Add this line to every template you want to protect with that password.

```
{% do craft.templateGuard.protect('Pa$$w0rd', 'secret-pages-group') %}
```

### Protect multiple entries each with their own password

[](#protect-multiple-entries-each-with-their-own-password)

Add this line to the template(s) rendering your entries.

```
{% do craft.templateGuard.protect(entry.myPasswordField, entry.uid) %}
```

> ❗️ Only templates with this line of code will be protected. Ensure your entry is not split across multiple pages.

### Add log out button

[](#add-log-out-button)

```

    {{ csrfInput() }}
    {{ actionInput('template-guard/sessions/delete') }}

    Log out

```

> ℹ️ Omit the hidden `key` input to log out from all template guard instances.

Configuration
-------------

[](#configuration)

Configure Template Guard via Settings → Plugins → Template Guard in the Craft Control Panel.

You can also create a configuration file: `config/template-guard.php`.

```
