0

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();
});
4
  • 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 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 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 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 at 11:39

0

Your Answer

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