DTW Single Media Updater
A WordPress plugin to update a single media file (PDF) by ID via an admin page with custom permissions.
by Your Name · github.com/swarnat/wordpress-single-media-upload · website
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/swarnat/wordpress-single-media-upload/archive/refs/heads/main.zipReadme
DTW Single Media Updater
A WordPress plugin to update a single media file (specifically a PDF) by ID via a dedicated admin page. This plugin is designed to allow users with specific permissions to easily replace a pre-defined PDF file in the WordPress media library without needing full media library access.
Features
- Dedicated Admin Page: A new menu item "Dienstplan" under the WordPress admin dashboard provides a simple interface for PDF uploads.
- Single File Replacement: Uploaded PDF files overwrite the existing media file associated with the configured Media ID, maintaining the original filename and URL.
- Custom Permissions: Access to the upload page is controlled by a custom capability (
manage_single_media_update), ensuring only authorized users can perform updates. - Configurable Media ID: The target Media ID can be easily changed on a separate settings page, accessible only by administrators.
Installation
- Upload: Upload the
dtw-singlemedia-updaterfolder to the/wp-content/plugins/directory of your WordPress installation. - Activate: Activate the plugin through the 'Plugins' menu in WordPress.
Configuration
After activating the plugin, administrators can configure the target Media ID:
- Navigate to Dienstplan > Settings in your WordPress admin sidebar.
- Target Media ID: Enter the ID of the existing PDF file in your WordPress media library that you wish to replace.
- Default: The default Media ID is
8531. You should change this to the actual ID of the PDF you intend to update. - How to find Media ID: You can find the Media ID by going to
Media > Library, clicking on the desired PDF file, and looking at the URL in your browser. The ID will be in thepost=parameter (e.g.,.../wp-admin/post.php?post=8531&action=edit).
- Default: The default Media ID is
- Click "Save Settings" to apply the changes.
Usage
Once configured, users with the manage_single_media_update capability can:
- Log in to the WordPress admin area.
- Navigate to Dienstplan in the sidebar.
- Upload a new PDF file using the provided form.
- Click "Upload and Replace PDF".
The old PDF associated with the configured Media ID will be replaced with the newly uploaded one.
Permissions
By default, the custom capability manage_single_media_update is assigned to the administrator role upon plugin activation.
If you need to grant this permission to other user roles (e.g., a custom role for "Dienstplan Uploader"), you can use a role editor plugin (like "User Role Editor") or add the capability programmatically:
// Example: Granting the capability to an 'editor' role
$role = get_role( 'editor' );
if ( $role ) {
$role->add_cap( 'manage_single_media_update' );
}
Development Notes
- Constants:
DTW_SMU_CAPABILITY: Defines the custom capability required to access the plugin's functionality.DTW_SMU_DEFAULT_MEDIA_ID: The initial default media ID used if no other is configured.
- Hooks:
admin_menu: Used to add the plugin's admin page.register_activation_hook: Adds the custom capability to administrators on plugin activation.register_deactivation_hook: (Optional) Removes the custom capability and cleans up options on deactivation.
Troubleshooting
- "You do not have sufficient permissions": Ensure the user role has the
manage_single_media_updatecapability. - "Invalid file type": Make sure you are uploading a PDF file.
- "Error replacing media file": Double-check that the "Target Media ID" is correct and corresponds to an existing media item.
- File not updating: Clear any caching plugins or server-side caches after updating the PDF.
License
GPL2