I want to upload images in a unique folder. Then retrieve the path of this image saved on server like


Now I want to save this URL in a post having custom field as url.

I'll be overwriting the posts on regular basis as well as replacing corresponding images, so I don't want to keep the images related to previous posts (deleted posts) on my server (otherwise it would increase the memory on server). I want to delete all the images from myimages folder. I don't want to change the default path for uploads. Here's my code for creating the post:

$new_post = array(
'post_title' => $phototitle,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'post',
'post_category' => array(328)


require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$image = media_sideload_image($photourl, $postid, $phototitle);

$path = (string)$image;
$arr = (explode("'",$path));
$url = $arr[1];


Please tell me how to change this upload path without making any change in wp-config.php or media settings.

2 Answers 2


Here is simple function to create your own sub directory for every upload.

function kv_custom_image_dir( $pathdata ) {
    $subdir = '/uploads_img'.$pathdata['subdir'];
    $pathdata['path'] = str_replace($pathdata['subdir'], $subdir, $pathdata['path']);
    $pathdata['url'] = str_replace($pathdata['subdir'], $subdir, $pathdata['url']);
    $pathdata['subdir'] = str_replace($pathdata['subdir'], $subdir, $pathdata['subdir']);
    return $pathdata;
add_filter( 'upload_dir', 'kv_custom_image_dir' );

From my above code, I changed my images folder as uploads_img, you can change yours likewise.

If you want more information, I wrote a tutorial for changing upload directories for every custom upload, take a look at my blog:


Notice: Haven't tested my idea or code provided below.

*media_sideload_image* calls *wp_handle_upload* function which, suprisingly, handles file upload.

Upload dir is set via *wp_upload_dir* function. This function has a filter which can be used to modify it's output (see http://core.trac.wordpress.org/browser/tags/3.6/wp-includes/functions.php#L1607):

$uploads = apply_filters( 'upload_dir',
    'path'    => $dir,
    'url'     => $url,
    'subdir'  => $subdir,
    'basedir' => $basedir,
    'baseurl' => $baseurl,
    'error'   => false,
) );

So hooking into that filter via:

add_filter( 'upload_dir', 'my_upload_dir_function' );

function my_upload_dir_function( $uploads ){
    if( isset( $_POST['my_check_post_var'] ) && $_POST['my_check_post_var'] == 'my_check_var_value' ){
        return array(
            'path'    => $dir, //have to be set
            'url'     => $url, //have to be set
            'subdir'  => $subdir, //have to be set
            'basedir' => $basedir, //have to be set
            'baseurl' => $baseurl, //have to be set
            'error'   => false,
        return $uploads;

The code pressume, that you have a hidden field in your code which set's a check var with specific value agains you check in your filter. If that $_POST var is not present or has a different value, you return original array - thus you won't break a standard image uploads.

And of course, you have to create all variables used in the filter, eg. $dir, $url etc. Check http://core.trac.wordpress.org/browser/tags/3.6/wp-includes/functions.php#L1525 to get and idea how WP does this.

  • I'm a beginner in wordpress, so now I'm learning this stuff. Please don't bother if I ask any stupid question. By the time can you please tell me where can I mention my new directory? Or should I mention it in the my_upload_dir_function's array?
    – Akshay
    Aug 30, 2013 at 12:36
  • yup, in that function... Aug 30, 2013 at 17:31

Your Answer

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

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