2 Improve code formatting / indentation, misc
source | link

For our internal webpage, we used the code from here and then made it use the top level parent if the upload ifis from a page. SinceSince it is internal, we didn't care about the username being visablevisible. ThisThis allowed us to break up the uploads by something close to departments.

function wpse_25894_custom_upload_dir( $path )
  {   
    /*
     * Determine if uploading from inside a page - if not, use the username
     */
    if ( ( strpos( $_SERVER['HTTP_REFERER'], 'wp-admin/upload' ) !== false ) || 
        ( strpos( $_SERVER['HTTP_REFERER'], 'wp-admin/media-new' ) !== false ) ){ 
    {  
        $current_user = wp_get_current_user();    
        $customdir = '/' . $current_user->user_login;
    
    }
  else {
        $post = get_post( $_REQUEST['post_id'] );
     
        /* 
 *If        * If there is a parent page, 
 * then get the hierarchy array, 
         * reverse it and use the first one to get the top level parent
         */
        if ( $post->post_parent ) {
            $parent = array_reverse( get_post_ancestors( $post->ID ) );
            $first_parent = get_page( $parent[0] );
            $customdir  = '/' . $first_parent->post_name;
        }
        /* 
         * Check if it is not a page (post or attachment page)
         * ifIf it is, then use the username
         */
        elseif (strcmp ( 'page', $page->type ) == false ) {
            $current_user = wp_get_current_user();    
            $customdir = '/' . $current_user->user_login;
        }
        /*
         * otherwiseOtherwise it is a top level parent page and just use the page name
         */
        else {
            $the_post = get_post( $_REQUEST['post_id'] );
            $customdir = '/' . $the_post->post_name;
        }
 
    }
 
    //format Format the variables into a useable path
    $path['path']    = str_replace( $path['subdir'], '', $path['path'] ); //remove Remove default subdir (year/month)
    $path['url']     = str_replace( $path['subdir'], '', $path['url'] );      
    $path['subdir']  = $customdir;
    $path['path']   .= $customdir; 
    $path['url']    .= $customdir;  
 
    return $path;
}

For our internal webpage, we used the code from here and then made it use the top level parent if the upload if from a page. Since it is internal, we didn't care about the username being visable. This allowed us to break up the uploads by something close to departments

function wpse_25894_custom_upload_dir($path)
 {   
    /*
     * Determine if uploading from inside a page - if not, use the username
     */
    if( (strpos($_SERVER['HTTP_REFERER'],'wp-admin/upload') !== false) || (strpos($_SERVER['HTTP_REFERER'],'wp-admin/media-new') !== false )){

 $current_user = wp_get_current_user();    
 $customdir = '/' . $current_user->user_login;
 
}
 else{
 $post = get_post($_REQUEST['post_id']);
 
 /* 
 *If there is a parent page, 
 * then get the hierarchy array, reverse it and use the first one to get the top level parent
 */
 if ( $post->post_parent ) {
  $parent = array_reverse(get_post_ancestors($post->ID));
  $first_parent = get_page($parent[0]);
  $customdir  = '/' . $first_parent->post_name;
 }
 /* 
 * Check if it is not a page (post or attachment page)
 * if it is, then use the username
 */
 elseif (strcmp ( 'page', $page->type) == false){
  $current_user = wp_get_current_user();    
  $customdir = '/' . $current_user->user_login;
 }
 /*
 * otherwise it is a top level parent page and just use the page name
 */
 else{
     $the_post = get_post($_REQUEST['post_id']);
     $customdir = '/' . $the_post->post_name;
 }
 
}
 
//format the variables into a useable path
   $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;
}

For our internal webpage, we used the code from here and made it use the top level parent if the upload is from a page. Since it is internal, we didn't care about the username being visible. This allowed us to break up the uploads by something close to departments.

function wpse_25894_custom_upload_dir( $path ) {   
    /*
     * Determine if uploading from inside a page - if not, use the username
     */
    if ( ( strpos( $_SERVER['HTTP_REFERER'], 'wp-admin/upload' ) !== false ) || 
        ( strpos( $_SERVER['HTTP_REFERER'], 'wp-admin/media-new' ) !== false ) ) 
    {  
        $current_user = wp_get_current_user();    
        $customdir = '/' . $current_user->user_login;   
    } else {
        $post = get_post( $_REQUEST['post_id'] );    
        /* 
         * If there is a parent page, then get the hierarchy array, 
         * reverse it and use the first one to get the top level parent
         */
        if ( $post->post_parent ) {
            $parent = array_reverse( get_post_ancestors( $post->ID ) );
            $first_parent = get_page( $parent[0] );
            $customdir  = '/' . $first_parent->post_name;
        }
        /* 
         * Check if it is not a page (post or attachment page)
         * If it is, then use the username
         */
        elseif (strcmp ( 'page', $page->type ) == false ) {
            $current_user = wp_get_current_user();    
            $customdir = '/' . $current_user->user_login;
        }
        /*
         * Otherwise it is a top level parent page and just use the page name
         */
        else {
            $the_post = get_post( $_REQUEST['post_id'] );
            $customdir = '/' . $the_post->post_name;
        }
    }
    // Format the variables into a useable path
    $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;
}
1
source | link

For our internal webpage, we used the code from here and then made it use the top level parent if the upload if from a page. Since it is internal, we didn't care about the username being visable. This allowed us to break up the uploads by something close to departments

function wpse_25894_custom_upload_dir($path)
{   
    /*
     * Determine if uploading from inside a page - if not, use the username
     */
    if( (strpos($_SERVER['HTTP_REFERER'],'wp-admin/upload') !== false) || (strpos($_SERVER['HTTP_REFERER'],'wp-admin/media-new') !== false )){

 $current_user = wp_get_current_user();    
 $customdir = '/' . $current_user->user_login;

}
else{
 $post = get_post($_REQUEST['post_id']);

 /* 
 *If there is a parent page, 
 * then get the hierarchy array, reverse it and use the first one to get the top level parent
 */
 if ( $post->post_parent ) {
  $parent = array_reverse(get_post_ancestors($post->ID));
  $first_parent = get_page($parent[0]);
  $customdir  = '/' . $first_parent->post_name;
 }
 /* 
 * Check if it is not a page (post or attachment page)
 * if it is, then use the username
 */
 elseif (strcmp ( 'page', $page->type) == false){
  $current_user = wp_get_current_user();    
  $customdir = '/' . $current_user->user_login;
 }
 /*
 * otherwise it is a top level parent page and just use the page name
 */
 else{
     $the_post = get_post($_REQUEST['post_id']);
     $customdir = '/' . $the_post->post_name;
 }

}

//format the variables into a useable path
   $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;
}