0

I am using Add From Server plugin http://wordpress.org/extend/plugins/add-from-server/ basically it imports/upload image from the server. Currently the plugin copies the images into month/year folder format but I want to change it by making it copy by the Post ID folder format instead of month/year.

I have already tried using the Custom Upload Dir plugin but it doesn't work with Add From Server plugin together. Can anyone tweak Add From Server plugin to make it upload by Post ID?

2 Answers 2

1
+50

You can try to add this into your functions.php file:

add_filter( 'upload_dir','wpse87234_upload_dir');
function wpse87234_upload_dir($uploads){

    // check if we are in the "Add From Server" media tab
    if (isset($_REQUEST['post_id']) && isset($_GET['tab']) && $_GET['tab']=="server") {
        $prefix="post_";
        $post_id=(int)$_REQUEST['post_id'];

        // change the uploads info to our needs:
        $uploads['subdir']="/".$prefix.$post_id;
        $uploads['path']=$uploads['basedir'].$uploads['subdir'];
        $uploads['url']=$uploads['baseurl'].$uploads['subdir'];
    }
    return $uploads;    
}

This should give directory like /wp-content/uploads/post_1234/ with post_id 1234.

2

Analyze

From the plugins source: it uses wp_upload_dir() and wp_insert_attachment(), which uses update_attached_file(). So basically the plugin should just incorporate your media settings. If it is not doing this, it has a conflict with either some other plugin, or your wp-config.php settings.

Filter callback for the rescue

You can still use the 'upload_dir'-filter. It takes one argument that has the following data:

array(
    'path'    => $dir,
    'url'     => $url,
    'subdir'  => $subdir,
    'basedir' => $basedir,
    'baseurl' => $baseurl,
    'error'   => false,
)

Just add a callback to it:

add_filter( 'upload_dir', 'wpse87234_custom_upload_dir' );
function wpse87234_custom_upload_dir( $data )
{
    // Change everything you need.
    return array_merge( $data, array(
        'path'    => $data['dir'],
        'url'     => $data['url'],
        'subdir'  => $data['subdir'],
        'basedir' => $data['basedir'],
        'baseurl' => $data['baseurl'],
        'error'   => false,
    ) );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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