To be better organized I must be able to manage the media independently of the user.

So in addition to restrict user to see only these files(images, docs, videos), I need to insert media for each organization inside a new uploads subfolder.ex: uploads/user-subfolder I don't want to use "author" as a basis for comparison because I must create posts for user before it exist and it is easier to define a media subfolder by user to be sure to restrict access media to each one and that can be later associated.

This subfolder is well created and works, but I can't find any explanation in documentation how to modify the directory of media uploaded with wp.media.frame

Inside custom post the User or me define a subfolder name. Then, this one must be used for the new directory to upload media.

So, opening a custom wp.media.frame this must be linked to an uploads subdirectory. It will be different for each organization.

I don't think that apply_filters( 'upload_dir', array $uploads ) can be used in my case because this filter will be apply for all without distinction.

Currently I use this jQuery script to open/close wp.media.frame .

 * Define media_lib_frame as wp.media object
const media_lib_frame = wp.media({
    title: 'Ajouter une image',
    multiple: false,
    library: {
        type: 'image',
        uploadedTo : wp.media.view.settings.post.id 
        // is this the parameter that defines the folder path ? 

 * On close, get selections and save to the hidden input
 * plus other AJAX stuff to refresh the image preview
media_lib_frame.on('close', _ => {
    const selection = media_lib_frame.state().get('selection');
    const gallery_ids = selection.map(attachment => attachment['id']);
    const ids = gallery_ids.join(",");
    if($('#idImage').val() == ''){
        $('#btnOpenMediaLibFrame').prepend('<span>Ajouter une image</span>');
        $('#idImage').before('<img id="image_preview">');

 * On open, get the id from the hidden input
 * and select the appropriate images in the media manager
media_lib_frame.on('open', _ => {
    $('#btnOpenMediaLibFrame span').remove();
    else{$('#btnOpenMediaLibFrame').append('<img id="image_preview">');}
    const selection = media_lib_frame.state().get('selection');
    const ids = jQuery('input#idImage').val().split(',');
    ids.forEach(id => {
        const attachment = wp.media.attachment(id);
        selection.add(attachment ? [attachment] : []);
    $('#btnOpenMediaLibFrame').click(e => {
  • the media library, and the JS UI you're modifying, has no control over the folder used to store the file. It's also not a filesystem viewer. It displays posts of type attachment and it's the PHP side of things that decides where the file is going to be stored. Likewise the media library won't know anything about the post, and if it does the PHP side will at best know the post ID, which may not exist yet if the post has not been saved, or the organisation set. This is why you can't find an explanation of how to modify the folder path from wp.media.frame, because you can't
    – Tom J Nowell
    Oct 13, 2023 at 16:16
  • Thanks for your answer @TomJNowell. Now I'm looking for to the author filter : Depending on your experience should I look this functionapply_filters( 'wp_handle_upload_prefilter', array $file ) or create a new wp.media.model.Attachments using query based on this documentation with * @param {Object} [props.author] ?
    – imagIne
    Oct 16, 2023 at 8:56
  • you should ask new questions as a new question, keep in mind these are just the comments section, nobody has written an answer on this question yet, those would appear below with voting controls and larger text with their own comment sections
    – Tom J Nowell
    Oct 16, 2023 at 11:22
  • I understand. I finally found an indirect solution adding the "author" parameter inside wp.media object : wp.media({library:{author:the_id_to_filter}});
    – imagIne
    Oct 16, 2023 at 11:39


Your Answer

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