Temporary Document Access
A WordPress plugin for private documents with one-hour, token-based temporary access links.
by Dmytro Kolisnyk · github.com/dmkolisnyk/temporary-document-access-plugin
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/dmkolisnyk/temporary-document-access-plugin/archive/refs/heads/master.zipTemporary Document Access is a WordPress plugin that adds a private Documents section and allows administrators to generate temporary access links for individual documents.
Each generated link contains a document ID, a token, and an expiration timestamp. The link is valid for 1 hour and can be used to view the document only while the token and expiration timestamp are valid.
Features
- Registers a private custom post type:
shared_document. - Supports only the document title and editor fields.
- Keeps documents hidden from public archives, search, and regular frontend URLs.
- Adds an Access Management metabox to the document editor.
- Generates temporary access links in the format:
https://example.com/?view_doc=[ID]&token=[HASH]&expires=[TIMESTAMP]
- Saves the generated URL, token, and expiration timestamp in post meta.
- Displays the latest generated link in the admin area for copying.
- Validates access by checking:
- the document ID;
- the expiration timestamp;
- the token generated from the document ID, expiration timestamp, and
wp_salt(); - the latest saved token and expiration timestamp.
- Shows the document through a dedicated template only when validation succeeds.
- Returns a
403error when the link is invalid or expired.
Requirements
- WordPress 6.x or newer.
- PHP 8.2 or newer.
Installation
- Copy the plugin folder to:
wp-content/plugins/temporary-document-access-plugin/
- Go to WordPress Admin → Plugins.
- Activate Temporary Document Access.
- Open Documents in the admin menu.
- Create or edit a document.
- Click Generate Link in the Access Management metabox.
- Copy the generated temporary link.
How It Works
The plugin registers a non-public custom post type called shared_document. Documents cannot be accessed through normal WordPress URLs because the post type is registered with:
'public' => false,
'has_archive' => false,
'publicly_queryable' => false,
Temporary access works through a custom query URL:
/?view_doc=[ID]&token=[HASH]&expires=[TIMESTAMP]
When a visitor opens this URL, the plugin:
- Reads and sanitizes the request parameters.
- Checks that the expiration timestamp has not passed.
- Recalculates the token using the document ID, expiration timestamp, and
wp_salt(). - Compares the provided token with the expected token using
hash_equals(). - Compares the provided token and expiration timestamp with the latest saved values in post meta.
- Loads the document template if validation passes.
- Stops the request with a
403error if validation fails.
Project Structure
temporary-document-access-plugin/
├── temporary-document-access.php
├── includes/
│ ├── class-plugin.php
│ ├── class-post-type.php
│ ├── class-admin.php
│ ├── class-token-service.php
│ └── class-frontend.php
├── templates/
│ └── document-view.php
└── README.md
Security Notes
- Admin link generation uses a nonce-protected
POSTrequest throughadmin-post.php. - The plugin checks user permissions with
current_user_can('edit_post', $post_id)before generating a link. - Request parameters are sanitized with WordPress helpers such as
absint(),sanitize_text_field(), andwp_unslash(). - Output is escaped with functions such as
esc_url(),esc_attr(), andesc_html__(). - Token comparison uses
hash_equals(). - The generated link is valid for 1 hour.
- Only the latest generated link remains valid for a document.