Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to find a way to upload few selective images to selective folders in WordPress but not able to find a way so far.

I do not want to change default work press setting to upload images as that is needed in all cases.

Working on a travel site where i need to show images of destinations on destination introduction page.Proposed structure.

  1. Each Destination will belong to a state.
  2. Each destinations can have multiple posts.

So for e.g user want to see details about Taj Mahal, so there can be multiple posts about Taj Mahal but will going to have a common landing page for Taj Mahal with a list of posts/articles about the place.

On landing page i want to show few images about that destination, and i want to store these images in following structure

wp-content\uploads\state-name\destination

PS:For each destination, there will be few introduction images (2-3 max), and i want to upload them to specific folder.

Is it possible or do i need to create these folder structure manually

share|improve this question
    
@G.M.: you observation is correct, for each destination there will be a intro image and i want to upload that to specific folder, so that i can pick it easily as per my requirement. –  Umesh Awasthi Sep 14 '13 at 8:18
    
Althoug what you need is different, maybe this post gives you inspiration. You can also can create a custom metabox to upload the images whatever you want.....just thinking in possibilities. –  cybmeta Sep 14 '13 at 8:40
    
@G.M.: My question is about how we can make this possible, like there might be some additional upload button and i can choose which directory to upload. –  Umesh Awasthi Sep 14 '13 at 9:00
    
they are CPT type –  Umesh Awasthi Sep 14 '13 at 9:45
    
@UmeshAwasthi you don't tag me in last commenst, so I miss it for a while. I answered you assuming 'states' are taxonomies, hoping it's right. –  G. M. Sep 14 '13 at 11:13

1 Answer 1

In your answer you said that desired path is something like

wp-content\uploads\state-name\destination\

where state-name is a taxonomy term and destination is the slug of one destination CPT. Last thing is not clear from your question, but it seems so, let me know if I'm wrong).

So, I suggest you this workflow:

Add a destination post -> assign a state term -> after that, upload the image using the media uploader from the destination post just created.

Doing so, we can retrieve the destination ID for current attachment (passed to media uploader as $_GET['post_id']), than retrieve the assigned state term, and finally set the folder using the 'upload_dir' filter.

Something easy:

add_filter('upload_dir', 'set_destination_folder', 999);

function set_destination_folder ( $upload_data ) {  

  if ( ! isset( $_REQUEST['post_id']) )
    return $upload_data;
  $destination = get_post( $_REQUEST['post_id'] );
  if ( empty($destination) || $destination->post_type != 'destinations' )
    return $upload_data;
  $states = get_the_terms($_REQUEST['post_id'], 'states');
  if ( empty($states) || ! is_array($states) ) {
    $subdir = '/destinations/' . $destination->post_name;
  } else {
    $subdir = '/' . array_shift($states)->slug . '/destinations/' . $destination->post_name;
  }
  $dir = $upload_data['basedir'] . $subdir;
  $url = $upload_data['baseurl'] . $subdir;
  return wp_parse_args(array('path'=>$dir, 'url'=>$url, 'subdir'=>$subdir), $upload_data);

}

In this way, when you upload images from a destination post, if the destination post as a state assigned (as taxonomy term) the image will uploaded in the folder

wp-content/{$state_slug}/destinations/{$destination_slug}/

if the destination post has no state assigned, the image will be saved in the folder

wp-content/destinations/{$destination_slug}/

If more states are assigned to destination, the first state is taken.

Note that this code assume the cpt name for destinations is, 'destinations' and the taxonomy name for states is 'states'.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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