WP Manifestindependent plugin directory
manifest / media / wordpress-single-media-upload

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

0stars
0forks

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.zip

Readme

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

  1. Upload: Upload the dtw-singlemedia-updater folder to the /wp-content/plugins/ directory of your WordPress installation.
  2. Activate: Activate the plugin through the 'Plugins' menu in WordPress.

Configuration

After activating the plugin, administrators can configure the target Media ID:

  1. Navigate to Dienstplan > Settings in your WordPress admin sidebar.
  2. 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 the post= parameter (e.g., .../wp-admin/post.php?post=8531&action=edit).
  3. Click "Save Settings" to apply the changes.

Usage

Once configured, users with the manage_single_media_update capability can:

  1. Log in to the WordPress admin area.
  2. Navigate to Dienstplan in the sidebar.
  3. Upload a new PDF file using the provided form.
  4. 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_update capability.
  • "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

Read the full README on GitHub →