Viewing 0 reply threads
  • Author
    Posts
    • #5288
      Mehdi Salem
      Member

      Seems like the post variable $_post[‘post_id’] is not sent to the file media uploader iframe when triggered from the by Piklist’s file upload metabox, when editing a post…

      As a consequence, a new file uploaded from a post via Piklist des not have a post parent set and the file cannot be attached the the current post by WP… while a native WP featured image will be attached with no issue to its post.

      Could you fix this issue please? I’m trying to set the upload directory dynamically based on the post_type of the parent post of attachments for my client so I need to be able to know from what post type the piklist uploader is triggered…

      Thank you!

      Here is my code:

      Custom Dynamic upload directory:

      function hdev_upload_media_dir_based_on_post_type($uploads)
      {
      	if(!isset($_POST['post_id'])) return $uploads; // do nothing if no attachment post parent is set
      
      	$post = get_post($_POST['post_id']);
      	$post_type = $post->post_type;
      	
      	$new_sub_dir = '/' . $post_type . $uploads['subdir'];
      
      	$uploads['path']    = str_replace($uploads['subdir'], $new_sub_dir, $uploads['path']);
      	$uploads['url']     = str_replace($uploads['subdir'], $new_sub_dir, $uploads['url']);
      	$uploads['subdir']  = str_replace($uploads['subdir'], $new_sub_dir, $uploads['url']); // not necessary, added just in case and it won't hurt anyway...
      
      	return $uploads;
      }
      function custom_upload_filter( $file )
      {
      	if(isset($_POST['action']) && $_POST['action'] === 'upload-attachment') add_filter('upload_dir', 'hdev_upload_media_dir_based_on_post_type'); // filter the upload directory and substitute with new dir
      
      	return $file;
      }
      add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
      

      Piklist file upload metabox:

      
      /*
      Title: <h3 class="hdev-highlight"><strong>Content</strong></h3>
      Post Type: post,sepia,black_and_white,color,conservation
      Order: 20
      */
      
      piklist('field', array(
          'type' => 'group'
          ,'field' => 'folder_content_group'
          ,'label' => __('Gallery Photo','stopping-the-world-admin')
          ,'description' => __('Add photos to this folder.', 'stopping-the-world-admin')
          ,'help' => __('Adds a new photo to this gallery folder and sets its availability on the store.','stopping-the-world-admin')
          ,'add_more' => true
          ,'fields' => array(
            array(
              'type' => 'file'
              ,'field' => 'single_photo'
              ,'label' => __('Photo','stopping-the-world-admin')
              ,'options' => array(
                  'modal_title' => __('Add a Photo','stopping-the-world-admin')
                  ,'button' => __('Add','stopping-the-world-admin')
              )
              ,'required' => true
              ,'validate' => array(
                  array(
                      'type' => 'limit',
                      'options' => array(
                              'min' => 1,
                              'max' => 1
                      )
                  )
                  /*,array(
                        'type' => 'image'
                  )*/
                  ,array(
                        'type' => 'jpeg'
                  )
              )
            )
            ,array(
              'type' => 'text'
              ,'field' => 'photo_title'
              ,'label' => __('Title','stopping-the-world-admin')
              ,'help' => __('Sets the photo title. This will appear bellow the photo and above the description on the Gallery Foolder post.','stopping-the-world-admin')
              ,'columns' => 7
              ,'attributes' => array(
                  'placeholder' => __('Enter photo title here...', 'stopping-the-world-admin')
              )
            )
            ,array(
              'type' => 'checkbox'
              ,'field' => 'store_availability'
              ,'label' => __('Store Availability','stopping-the-world-admin')
              ,'help' => __('(Leave unchecked if you do not want to sell this photo.)','stopping-the-world-admin')
              ,'columns' => 4
              ,'choices' => array(
                  'option1' => 'Stock'
                  ,'option2' => 'Print'
              )
            )
            ,array(
              'type' => 'textarea'
              ,'field' => 'photo_description'
              ,'label' => __('Description','stopping-the-world-admin')
              ,'help' => __('Sets the photo description. This will appear bellow the photo and its title on the Gallery Folder post.','stopping-the-world-admin')
              ,'attributes' => array(
                  'rows' => 5
                  ,'cols' => 120
                  ,'placeholder' => __('Describe this photo...', 'stopping-the-world-admin')
                  ,'maxlength' => '320'
              )
            )
           )
      ));
      
      Attachments:
      You must be logged in to view attached files.
Viewing 0 reply threads
  • You must be logged in to reply to this topic.