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(",");
$('input#idImage').val(ids);
loadImage(ids);
if($('#idImage').val() == ''){
$('#image_preview').remove('');
$('#btnOpenMediaLibFrame').prepend('<span>Ajouter une image</span>');
}
if($('#image_preview').length){}else{
$('#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();
if($('#image_preview').length){}
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);
attachment.fetch();
selection.add(attachment ? [attachment] : []);
});
});
//$('#btnImage')
$('#btnOpenMediaLibFrame').click(e => {
e.preventDefault();
media_lib_frame.open();
});
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 fromwp.media.frame
, because you can'tapply_filters( 'wp_handle_upload_prefilter', array $file )
or create a newwp.media.model.Attachments
using query based on this documentation with *@param {Object} [props.author]
?wp.media({library:{author:the_id_to_filter}});