plugins – Change upload folder for a CPT


I have a CPT called “Musterkuechen”. Users have access to a custom dashboard that I created with ACF Frontend. They do not have access to the wordpress backend.

When I create a new post in the backend of wordpress, all media ends up in the folder “musterkuechen”.

However, when a User create a new post using the custom dashboard, the media goes into the standard uploads folder.
My script works in the wordpress backend dashboard, but not in the custom dashboard that I’ve made for my users.

What did I miss to make it work in the custom user dashboard?

Here is the Code:

add_filter('upload_dir', 'rrwd_upload_dir');

$upload = wp_upload_dir();
// remove_filter('upload_dir', 'rrwd_upload_dir');

function rrwd_upload_dir( $upload ) {

$id = $_REQUEST['post_id'];
  $parent = get_post( $id )->post_parent;

  // Check the post-type of the current post
  if( "musterkuechen" == get_post_type( $id ) || "musterkuechen" == get_post_type( $parent ) )
    $upload['subdir'] = '/musterkuechen' . $upload['subdir'];

  $upload['path'] = $upload['basedir'] . $upload['subdir'];
  $upload['url']  = $upload['baseurl'] . $upload['subdir'];

  return $upload;

Ferkan 2 months 2021-05-08T21:16:38-05:00 0 Answers 0 views 0

Leave an answer