0

For my project I need to administrate user post and media without access to it personal account. So I must be able to create and edit a post (by is user name) before or after it has been created on behalf of a user.

On the head of custom post (custom plugin and specific wpdb table) I inserted a dropdown "select" based on wp_dropdown_users(); to choose the author of media (that is also the user ID) that will be used to create or edit content of specific user.

Currently I can set the author ID (user ID) to the wp.media frame to display media from the selected author, but I can't find the parameter to specify which author to upload a new media. The 'author' parameter is located in wp.media.options.library object and it concerns only the Media library not the upload frame/tab.

Can wp_set_current_user() do the job ? Can it be used to allow administrator to create or edit on behalf of specific user and overwrite wp.media parameters to open and upload media ? Otherwise Is there a function/hook/filter that would allow this only for custom plugin page/post and super admin or administrator?

Somebody can explain me how works wp.media object and how it defined the user and author that owns media? Does it use the session cookie and/or the user Logged in cookie?

Wordpress wp.media is poorly documented and the GitHub wp jsdocs is too difficult To be assimilated.

function generate_mediaframe(userId){
        $('#btnOpenMediaLibFrame').click(e => {
            e.preventDefault();
            var  media_lib_frame = wp.media({
                multiple: false,
                library: {
                    type: 'image',
                    uploadedTo : wp.media.view.settings.post.id,
                    author: userId
                }
            });
            media_lib_frame.open();
            $(e.target).addClass('ActiveFrame');
        });
}
4
  • wp_set_current_user is not the solution you are looking for and has nothing to do with this. This has nothing to do with the role of the user, what's likely happened is you've added an author parameter expecting it to set the author on the off-chance that it existed and was supported, but it isn't and doesn't exist or has a different ID
    – Tom J Nowell
    Oct 18, 2023 at 12:28
  • confirmed, I can find no evidence that the code you posted supports or has ever supported an author field for this use case, and that if it did it would be for filtering what was shown in the media library. The wp.media call does not provide any ways to influence the attributes of uploaded media once the frame is open in that way. Additionally, wp_set_current_user is a PHP function not a JS function, and it's purpose is to set the current users session, not to specify the author of an attachment post
    – Tom J Nowell
    Oct 18, 2023 at 12:31
  • About wp_set_current_user I was expecting to add an exception in session cookie to authorize SuperAdmin or Administrator to create and edit on behalf of user. I understand this is not its function at all.
    – imagIne
    Oct 18, 2023 at 12:58
  • you'd still need to communicate the user to set to the AJAX handler, at which point why not set it when inserting the post and avoid using it? Doing that would still require sending the author ID with the AJAX request from the media library, which leads you straight back to square one
    – Tom J Nowell
    Oct 18, 2023 at 14:28

1 Answer 1

0

Somebody can explain me how works wp.media object and how it defined the user and author that owns media? Does it use the session cookie and/or the user Logged in cookie?

This is not something that can be done with the wp.media call, not attributes will allow this or can be used to set this. This approach is a dead end.

Instead of trying to brute force the media library code into doing what you want, instead it would be much easier to do this:

When the media library has picked an image, ensure it has an author ID of X

Now the problem becomes a simple 2 step part which is much easier:

  1. How do I retrieve the ID of the selected attachment form the media frame?
  2. Given an attachment ID and a user ID, how do I set the user as the attachments author?

The first one is demonstrated in almost every tutorial of how to use wp.media to create an uploader and is the most common use case and best documented part of the code, and one you likely already have implemented.

The second has many options, ranging from creating your own AJAX handlers, using the REST API media endpoint to update the post author, to using wp.media itself!

In theory something like this might work:

attachment = wp.media.attachment( imageId );
attachment.fetch();
if ( desiredUserID != attachment.attributes.author ) {
    attachment.attributes.author = desiredUserID;
    attachment.save();
}

Note that if the user is able to pick images that have already been uploaded this means they can take over other peoples posts. This also will require the user to be able to make an authenticated request and can edit other users attachments, which won't be an issue for an admin but might be elsewhere if you expand on this.

The important thing, is that you don't have to mess with the internals of wp.media to do it all in one step. It would be nice if you could, but that doesn't mean it's the only method available to you.

13
  • Thanks a lot for these explanations . Is this documentation page correspond to your suggestion ?
    – imagIne
    Oct 18, 2023 at 13:21
  • unknown, what you've linked to is a description of the Attachment class that implements a backbone model, none of which I was aware of when I wrote my answer. If wp.media.attachment returns an object of that type then yes, but I do not have the answer to that, you will need to test and verify this yourself. Be mindful that by even asking that question you've put yourself in the top 0.01% of WP developers familiar with the Media libraries internals, very very few people understand the entire system and how it all fits together past a certain point
    – Tom J Nowell
    Oct 18, 2023 at 14:22
  • for reference, I arrived at the above answer by searching and this was the top result: dev.to/kelin1003/… I spotted wp.media.attachment in the code examples, opened up the browser console and executed it, then saw it had an attributes fetch and save method. If I were in your shoes the other option I would have taken was using the REST API directly in conjunction via fetch, especially if I only had to create and set the attachment, and had no need of a picker UI
    – Tom J Nowell
    Oct 18, 2023 at 14:25
  • I'm really a long way from being at this level since you're guiding me through my learning process. But this is the only reference I could find for customising wp.media frame. What do you think about wp_prepare_attachment_for_js() ? ' I'm not sure about it functionality. It talks about json encoding and I suppose it's for ajax/Rest use, am I wrong? I'm going to try out all your advice.
    – imagIne
    Oct 18, 2023 at 14:44
  • that's a PHP function, it wouldn't let you modify wp.media's post author. If you want a PHP based solution you would still need to communicate which user you want as the author and have that stored somewhere you can retrieve it from a PHP filter. At that point you could filter the creation of the attachment, but this is still completely backwards. wp.media is running in the browser in javascript, wp_prepare_attachment_for_js is a PHP function on the server that takes an attachment and generates JSON output. It's possibly used in the response to the upload handler after the upload occurs
    – Tom J Nowell
    Oct 18, 2023 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.