Created
April 17, 2025 03:00
-
-
Save arenagroove/8f881579c8934f8694f6945351cc92ca to your computer and use it in GitHub Desktop.
MU Plugin to upload files with specified prefixes to specific folders.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: LR Custom Media Upload Directory | |
| * Description: A must-use plugin to upload files with specified prefixes to specific folders. | |
| * Version: 2.1 | |
| * Author: Luis Martinez | |
| * Author URI: https://www.lessrain.com | |
| */ | |
| if (!defined('ABSPATH')) { | |
| exit; | |
| } | |
| // Add settings page to WordPress Tools menu | |
| function lr_cmu_add_admin_menu() { | |
| add_management_page('LR Custom Media Upload Settings', 'LR Custom Media Upload Settings', 'manage_options', 'lr-custom-media-upload-settings', 'lr_cmu_options_page'); | |
| } | |
| add_action('admin_menu', 'lr_cmu_add_admin_menu'); | |
| // Register settings | |
| function lr_cmu_settings_init() { | |
| register_setting('lr_cmuSettings', 'lr_cmu_mappings', [ | |
| 'type' => 'array', | |
| 'sanitize_callback' => 'lr_cmu_sanitize_mappings', | |
| 'default' => [] | |
| ]); | |
| register_setting('lr_cmuSettings', 'lr_cmu_plugin_enabled'); | |
| } | |
| add_action('admin_init', 'lr_cmu_settings_init'); | |
| // Sanitize input mappings | |
| function lr_cmu_sanitize_mappings($input) { | |
| $sanitized_mappings = []; | |
| if (is_array($input)) { | |
| foreach ($input as $mapping) { | |
| if (!empty($mapping['prefix']) && !empty($mapping['directory'])) { | |
| $sanitized_mappings[] = [ | |
| 'enabled' => isset($mapping['enabled']) ? 1 : 0, | |
| 'prefix' => sanitize_text_field($mapping['prefix']), | |
| 'directory' => sanitize_text_field($mapping['directory']), | |
| ]; | |
| } | |
| } | |
| } | |
| return $sanitized_mappings; | |
| } | |
| // Render settings page | |
| function lr_cmu_options_page() { | |
| $mappings = get_option('lr_cmu_mappings', []); | |
| $plugin_enabled = get_option('lr_cmu_plugin_enabled', 1); | |
| ?> | |
| <div class="wrap" style="max-width: 900px;"> | |
| <h1>LR Custom Media Upload Settings</h1> | |
| <form action="options.php" method="post"> | |
| <?php settings_fields('lr_cmuSettings'); ?> | |
| <h2>General Settings</h2> | |
| <hr> | |
| <label> | |
| <input type="checkbox" name="lr_cmu_plugin_enabled" value="1" <?php checked(1, $plugin_enabled, true); ?>> Enable Plugin | |
| </label> | |
| <hr> | |
| <h2>Prefix to Folder Mappings</h2> | |
| <div id="lr_cmu_mappings_container"> | |
| <?php foreach ($mappings as $index => $mapping) { ?> | |
| <div class="lr-cmu-mapping" style="display: flex; align-items: center; gap: 10px; padding: 10px; border-bottom: thin solid #ddd;"> | |
| <label><input type="checkbox" class="mapping-enable" name="lr_cmu_mappings[<?php echo $index; ?>][enabled]" value="1" <?php checked(1, $mapping['enabled'], true); ?>></label> | |
| <input type="text" class="regular-text mapping-field" name="lr_cmu_mappings[<?php echo $index; ?>][prefix]" value="<?php echo esc_attr($mapping['prefix']); ?>" placeholder="File Prefix" <?php echo !$mapping['enabled'] ? 'readonly style="opacity: 0.5;"' : ''; ?>> | |
| <input type="text" class="regular-text mapping-field" name="lr_cmu_mappings[<?php echo $index; ?>][directory]" value="<?php echo esc_attr($mapping['directory']); ?>" placeholder="Target Directory" <?php echo !$mapping['enabled'] ? 'readonly style="opacity: 0.5;"' : ''; ?>> | |
| <button type="button" class="button button-secondary remove-mapping">Remove</button> | |
| </div> | |
| <?php } ?> | |
| </div> | |
| <hr> | |
| <button type="button" id="add-mapping" class="button button-secondary">Add Mapping</button> | |
| <br><br> | |
| <?php submit_button(); ?> | |
| </form> | |
| </div> | |
| <script> | |
| document.addEventListener('DOMContentLoaded', function () { | |
| function toggleFields(checkbox) { | |
| let container = checkbox.closest('.lr-cmu-mapping'); | |
| let fields = container.querySelectorAll('.mapping-field'); | |
| fields.forEach(field => { | |
| field.readOnly = !checkbox.checked; // Use readonly instead of disabled | |
| field.style.opacity = checkbox.checked ? "1" : "0.85"; | |
| }); | |
| } | |
| document.querySelectorAll('.mapping-enable').forEach(checkbox => { | |
| checkbox.addEventListener('change', function () { | |
| toggleFields(this); | |
| }); | |
| }); | |
| document.getElementById('add-mapping').addEventListener('click', function () { | |
| let container = document.getElementById('lr_cmu_mappings_container'); | |
| let index = container.children.length; | |
| let div = document.createElement('div'); | |
| div.className = "lr-cmu-mapping"; | |
| div.style.display = "flex"; | |
| div.style.alignItems = "center"; | |
| div.style.gap = "10px"; | |
| div.style.padding = "10px"; | |
| div.style.borderBottom = "thin solid #ddd"; | |
| div.innerHTML = ` | |
| <label><input type="checkbox" class="mapping-enable" name="lr_cmu_mappings[\${index}][enabled]" value="1" checked></label> | |
| <input type="text" class="regular-text mapping-field" name="lr_cmu_mappings[\${index}][prefix]" placeholder="File Prefix"> | |
| <input type="text" class="regular-text mapping-field" name="lr_cmu_mappings[\${index}][directory]" placeholder="Target Directory"> | |
| <button type="button" class="button button-secondary remove-mapping">Remove</button>`; | |
| container.appendChild(div); | |
| div.querySelector('.mapping-enable').addEventListener('change', function () { | |
| toggleFields(this); | |
| }); | |
| }); | |
| document.getElementById('lr_cmu_mappings_container').addEventListener('click', function (e) { | |
| if (e.target.classList.contains('remove-mapping')) { | |
| e.target.closest('.lr-cmu-mapping').remove(); | |
| } | |
| }); | |
| }); | |
| </script> | |
| <?php | |
| } | |
| // Modify upload directory based on prefix mappings | |
| function lr_cmu_upload_directory($uploads) { | |
| if (!get_option('lr_cmu_plugin_enabled', 1)) { | |
| return $uploads; // Plugin disabled | |
| } | |
| $mappings = get_option('lr_cmu_mappings', []); | |
| if (isset($_FILES['async-upload'])) { | |
| $filename = $_FILES['async-upload']['name']; | |
| // Extract filename without extension | |
| $nameWithoutExtension = pathinfo($filename, PATHINFO_FILENAME); | |
| foreach ($mappings as $mapping) { | |
| if (!empty($mapping['enabled']) && strpos($nameWithoutExtension, $mapping['prefix']) !== false) { | |
| $directory = trim($mapping['directory']); | |
| $uploads['subdir'] = '/' . $directory; | |
| $uploads['path'] = $uploads['basedir'] . '/' . $directory; | |
| $uploads['url'] = $uploads['baseurl'] . '/' . $directory; | |
| if (!is_dir($uploads['path'])) { | |
| wp_mkdir_p($uploads['path']); | |
| } | |
| return $uploads; // Stop after first match | |
| } | |
| } | |
| } | |
| return $uploads; | |
| } | |
| add_filter('upload_dir', 'lr_cmu_upload_directory', 20); // Ensure it runs after filename modifications | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment