Getting post type and post-author from upload screen
-
I am writing a plugin that will create a custom media upload directory structure, based on whether a user is of a custom role (artist) or is of a custom post type (edg-portfolios).
I want users “artist” to be able to upload files to their own media folder (“uploads/artist-*their user ID#*”). Artists are allowed to upload to a custom post type from a CBM2 file upload meta, and they can upload a profile meta photo on their user profile page.Admins need to be able to add to the artist’s files too, but have the files added to the artist’s folder, and not to the admin’s.
The problem is detecting the user ID and the custom post type from the upload screen. The screen being used on my custom post type upload seems to be “asynch-upload.php” and I can’t find a way to access the data to test whether the upload is part of the edg-portfolios CPT.
Does anyone know how to get the post type from an upload screen? Also, are there other types of upload screens I should be aware of?
add_filter('wp_handle_upload_prefilter', 'wpse_25894_handle_upload_prefilter'); add_filter('wp_handle_upload', 'wpse_25894_handle_upload'); function wpse_25894_handle_upload_prefilter( $file ) { add_filter('upload_dir', 'wpse_25894_custom_upload_dir'); return $file; } function wpse_25894_handle_upload( $fileinfo ) { remove_filter('upload_dir', 'wpse_25894_custom_upload_dir'); return $fileinfo; } function wpse_25894_custom_upload_dir($path) { if(isset($_GET['post'])) { $the_post = get_post(); } if(isset($_POST['post_ID'])) { $the_post = $_POST['post_ID']; } global $current_user; $customdir = '/artist-'; if ( current_user_can('artist') ) { $customdir .= $current_user->ID; //artist can only manage his own pages, so no further tests needed. } elseif (current_user_can( 'manage_options' )) { //admin can manage an artist page, but we want files put with artist's upload dir, not admin's global $profileuser; $thisscreen = get_current_screen(); if (($thisscreen->base) == 'user-edit') { $profile_sub_role = implode(', ', $profileuser->roles); if (strpos($profile_sub_role, 'artist') !== false) { $customdir .= $profileuser->ID; } // THIS NEXT STEP DOESNT WORK } elseif ( ($thisscreen->base) == 'post' && ($thisscreen->post_type) == 'edg_portfolios' ) { $customdir .= $the_post->post_author; } } if ($customdir !== '/artist-') { $path['path'] = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month) $path['url'] = str_replace($path['subdir'], '', $path['url']); $path['subdir'] = $customdir; $path['path'] .= $customdir; $path['url'] .= $customdir; } return $path; }
- The topic ‘Getting post type and post-author from upload screen’ is closed to new replies.