Home / Attachments / change media owner
Duplicate Snippet

Embed Snippet on Your Site

change media owner

Allows the admin to assign any media library file from one user to another

<10
Code Preview
php
<?php
// Add a custom column in the Media Library list view
function custom_media_columns($columns) {
    // Check if the current user is an administrator
    if (current_user_can('manage_options')) {
        $columns['change_author'] = 'Author';
    }
    return $columns;
}
add_filter('manage_media_columns', 'custom_media_columns');
// Display the 'Change Author' link for each media file
function custom_media_custom_column($column_name, $id) {
    if ($column_name == 'change_author') {
        echo '<a href="#" class="change-author" data-attachment-id="' . $id . '">Change</a>';
        echo '<div class="change-author-form" data-attachment-id="' . $id . '" style="display:none;">';
        wp_dropdown_users(array('name' => 'attachment_author_' . $id, 'selected' => get_post_field('post_author', $id)));
        echo ' <button class="save-author">Save</button></div>';
    }
}
add_action('manage_media_custom_column', 'custom_media_custom_column', 10, 2);
// Enqueue JavaScript to handle the Change Author dropdown
function custom_media_admin_scripts() {
    if ('upload.php' == basename($_SERVER['PHP_SELF'])) {
        echo <<<JS
        <script type="text/javascript">
        document.addEventListener('DOMContentLoaded', function() {
            document.querySelectorAll('.change-author').forEach(function (elem) {
                elem.addEventListener('click', function(e) {
                    e.preventDefault();
                    var attachmentId = e.target.dataset.attachmentId;
                    var form = document.querySelector('.change-author-form[data-attachment-id="' + attachmentId + '"]');
                    form.style.display = 'inline';
                });
            });
            document.querySelectorAll('.save-author').forEach(function (elem) {
                elem.addEventListener('click', function(e) {
                    e.preventDefault();
                    var form = e.target.parentElement;
                    var attachmentId = form.dataset.attachmentId;
                    var select = form.querySelector('select');
                    var selectedUserId = select.value;
                    fetch(ajaxurl, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        },
                        body: 'action=update_attachment_author&attachment_id=' + attachmentId + '&user_id=' + selectedUserId + '&_wpnonce=' + wpApiSettings.nonce
                    }).then(function() {
                        location.reload();
                    });
                });
            });
        });
        </script>
JS;
    }
}
add_action('admin_footer', 'custom_media_admin_scripts');
// Handle AJAX request to update the attachment author
function update_attachment_author() {
    check_ajax_referer('wp_rest', '_wpnonce');
    if (!current_user_can('edit_others_posts')) {
        wp_send_json_error('You do not have permission to change the author of media files.');
    }
    $attachment_id = intval($_POST['attachment_id']);
    $user_id = intval($_POST['user_id']);
    wp_update_post(array('ID' => $attachment_id, 'post_author' => $user_id));
    wp_send_json_success('Attachment author updated successfully.');
}
add_action('wp_ajax_update_attachment_author', 'update_attachment_author');

Comments

Add a Comment