Crocoblock / suggestions

The suggestions for CrocoBlock project
191 stars 78 forks source link

JetEngine - Filter for changing upload path for media fields #7509

Open edgb0lw opened 1 month ago

edgb0lw commented 1 month ago

It's currently possible in ACF to change the uplaod path for a media field with a filter. So that the file does not end up in the standard WordPress folder /wp-content/uploads/... but that you can define a custom value.

I already had a few use cases for this, e.g. that certain images should all end up in a folder so that they're not indexed. Then you can simply say (with robots.txt) that this folder should be ignored by search engines. Or I had built another solution so that certain files could only be downloaded by logged-in users by forwarding all requests for the files to my .htaccess beforehand, which in turn validated the request with another PHP script of mine.

You simply cannot always expect the customer to use an FTP client for this. Most people are simply not that tech savvy. It'd also save me a lot of time.

Can you please give us this option?

This is how it currently works in ACF:

add_filter( 'acf/upload_prefilter/name=my-media-field-with-custom-path', function( $errors ) {
    add_filter( 'upload_dir', function( $uploads ) {
        $uploads[ 'path' ] = $uploads[ 'basedir' ] . '/my/custom/path';
        $uploads[ 'url' ] = $uploads[ 'baseurl' ] . '/my/custom/path';
        $uploads[ 'subdir' ] = '';

        return $uploads;
    }, 10, 1 );

    return $errors;
}, 10, 1 );

Thank you in advance! :) Edgar