0

Working with wordpress 4.2.2, when I use this code in child theme function.php to change the attachments upload directory according to each post type :

function wpse_16722_type_upload_dir( $args ) {
// Get the current post_id
$id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : '' );
if( $id ) {    
   // Set the new path depends on current post_type
   $newdir = '/' . get_post_type( $id );
   $args['path']    = str_replace( $args['subdir'], '', $args['path'] ); //remove default subdir
   $args['url']     = str_replace( $args['subdir'], '', $args['url'] );      
   $args['subdir']  = $newdir;
   $args['path']   .= $newdir; 
   $args['url']    .= $newdir; 
   return $args;
   }
}
add_filter( 'upload_dir', 'wpse_16722_type_upload_dir' );

I get a blank page!! there is no empty rows in the function.php and the problem appears only after adding this code. I really need it to organize my upload folder, but without a blank page. is there any solution?

  • Set debug to true – Pieter Goosen May 16 '15 at 3:27
  • Check error_logs and like @PieterGoosen said, enable debug in wp-config to see what is wrong. – Wali Hassan May 16 '15 at 7:02
0

You need a better function, your paths are broken - see this answer for reference:

function wpse_16722_type_upload_dir( $args ) {
    if ( ! empty( $_REQUEST['post_id'] ) && $post_id = absint( $_REQUEST['post_id'] ) ) {
        if ( $post = get_post( $post_id ) ) {               
            if ( $post->post_type !== 'attachment' ) {
                $args['subdir'] = "/$post->post_type"; // Must be preceded with slash
                $args['path']   = $args['basedir'] . $args['subdir'];
                $args['url']    = $args['baseurl'] . $args['subdir'];
            }
        }
    }

    return $args;
}
  • Erreur file.pdf The file could not be moved to wp-content/uploads/communications. – nisr May 16 '15 at 11:45
  • Permissions error. Make sure uploads is writable by PHP. Worst case scenario, set to 777. – TheDeadMedic May 16 '15 at 11:47
  • permissions set to 777 but error still happening – nisr May 16 '15 at 11:50
  • Have you recursively set 777? i.e. communications needs to be 777 too. – TheDeadMedic May 16 '15 at 11:51
  • yes i did that but error still – nisr May 16 '15 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.