* Author URI: https://gnilebein.de
* Text Domain: uuid-file-renamer
* License: GPL2
*/
// Hook in den Upload-Prozess
add_filter('wp_handle_upload_prefilter', 'rename_file_to_uuid');
function rename_file_to_uuid($file)
{
$uuid = wp_generate_uuid4();
$file_info = pathinfo($file['name']);
$extension = isset($file_info['extension']) ? '.' . $file_info['extension'] : '';
$file['name'] = $uuid . $extension;
return $file;
}
// Medienbearbeitungsseite erweitern
add_filter('attachment_fields_to_edit', 'add_uuid_rename_button', 10, 2);
function add_uuid_rename_button($form_fields, $post) {
$form_fields['uuid_rename'] = array(
'label' => __('UUID umbenennen', 'uuid-file-renamer'),
'input' => 'html',
'html' => ''
);
return $form_fields;
}
// Link in der Mediathek hinzufügen
add_filter('media_row_actions', 'add_uuid_rename_link', 10, 2);
function add_uuid_rename_link($actions, $post) {
if ($post->post_type === 'attachment') {
$actions['rename_to_uuid'] = '' . __('In UUID umbenennen', 'uuid-file-renamer') . '';
}
return $actions;
}
// AJAX-Handler für Einzel-Umbenennung
add_action('wp_ajax_rename_media_to_uuid', 'rename_media_to_uuid_ajax');
function rename_media_to_uuid_ajax() {
if (!current_user_can('manage_options')) {
wp_send_json_error(__('Keine Berechtigung.', 'uuid-file-renamer'));
}
if (!isset($_POST['attachment_id'])) {
wp_send_json_error(__('Fehlende Anhangs-ID.', 'uuid-file-renamer'));
}
$attachment_id = intval($_POST['attachment_id']);
rename_existing_media_to_uuid($attachment_id);
wp_send_json_success(__('Datei erfolgreich umbenannt.', 'uuid-file-renamer'));
}
// Medienübersicht erweitern
add_filter('bulk_actions-upload', 'register_bulk_uuid_rename');
function register_bulk_uuid_rename($bulk_actions) {
$bulk_actions['rename_to_uuid'] = __('In UUID umbenennen', 'uuid-file-renamer');
return $bulk_actions;
}
// Batch-Umbenennung verarbeiten
add_filter('handle_bulk_actions-upload', 'handle_bulk_uuid_rename', 10, 3);
function handle_bulk_uuid_rename($redirect_to, $doaction, $attachment_ids) {
if ($doaction !== 'rename_to_uuid') {
return $redirect_to;
}
foreach ($attachment_ids as $attachment_id) {
rename_existing_media_to_uuid($attachment_id);
}
$redirect_to = add_query_arg('bulk_uuid_renamed', count($attachment_ids), $redirect_to);
return $redirect_to;
}
// Funktion zum Umbenennen bestehender Medien inkl. Thumbnails
function rename_existing_media_to_uuid($attachment_id) {
$uuid = wp_generate_uuid4();
$file_path = get_attached_file($attachment_id);
$file_info = pathinfo($file_path);
$new_file_path = $file_info['dirname'] . '/' . $uuid . '.' . $file_info['extension'];
// Metadaten abrufen
$metadata = wp_get_attachment_metadata($attachment_id);
$upload_dir = wp_upload_dir();
if (rename($file_path, $new_file_path)) {
update_attached_file($attachment_id, $new_file_path);
wp_update_post(array(
'ID' => $attachment_id,
'post_title' => $uuid,
'post_name' => $uuid
));
// Thumbnails umbenennen, falls vorhanden
if (!empty($metadata['sizes'])) {
foreach ($metadata['sizes'] as $size => $data) {
$old_thumb_path = $upload_dir['basedir'] . '/' . dirname($metadata['file']) . '/' . $data['file'];
if (file_exists($old_thumb_path)) {
$thumb_info = pathinfo($old_thumb_path);
$new_thumb_path = $thumb_info['dirname'] . '/' . $uuid . '-' . $data['width'] . 'x' . $data['height'] . '.' . $thumb_info['extension'];
rename($old_thumb_path, $new_thumb_path);
$metadata['sizes'][$size]['file'] = basename($new_thumb_path);
}
}
wp_update_attachment_metadata($attachment_id, $metadata);
}
}
}
// JS für Buttons hinzufügen
add_action('admin_footer', 'uuid_renamer_js');
function uuid_renamer_js() {
echo '';
}