logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$55
Custom upload location for p-type not changing attachment data

Hi,

I've created a custom post type, with custom taxonomy's, and a custom upload location for media uploaded on my custom post type.

All seems to work fine, but one problem.

The thumbnail does not show in my 'Add an Image' pop-up. See here http://i.imgur.com/GM260.png

The image inserts fine into my post with the correct URL etc. And my thumbnails display fine on Posts and Pages, but if I add it as an image link, the link is still using the old upload directory?

-----------------------------------------------------

UPDATE

I just realised I also have another problem which is why the above question is happening.

My custom filter add_filter( 'upload_dir', 'my_custom_upload_dir' ); which only applies for my custom post-type called 'download', only actually changes the location of where the file is saved. This is not what I thought it should do :/

I need the location of my upload to be changed (as per above filter) but also all the attachment data to correspond with this same location, which the above filter does not do, it only does the upload location.

If I add an image into my post (inside my custom post-type 'download'), then I need all the attachment data to go to that file, instead of my default upload directory. For example if I add an image into my post and make it a link to that file, the link it's using is the default directory.

Person to submit working code for functions.php to change the attachment data for only the custom post-type 'download' to the same location as my 'upload_dir' filter will get ALL the money!

Please don't hesitate to ask me any questions.

Thanks


See below my functions.



// Downloads Post Type
add_action( 'init', 'create_post_type' );
function create_post_type() {
$args = array(
'labels' => post_type_labels( 'Download' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'taxonomies' => array( 'group' ),
'menu_icon' => '/wp/wp-content/plugins/download-monitor/img/menu_icon.png',
'supports' => array('title',
'editor',
'author',
'thumbnail',
'excerpt',
'comments'
)
);
register_post_type( 'download', $args );
}

function post_type_labels( $singular, $plural = '' )
{
if( $plural == '') $plural = $singular .'s';

return array(
'name' => _x( $plural, 'post type general name' ),
'singular_name' => _x( $singular, 'post type singular name' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New '. $singular ),
'edit_item' => __( 'Edit '. $singular ),
'new_item' => __( 'New '. $singular ),
'view_item' => __( 'View '. $singular ),
'search_items' => __( 'Search '. $plural ),
'not_found' => __( 'No '. $plural .' found' ),
'not_found_in_trash' => __( 'No '. $plural .' found in Trash' ),
'parent_item_colon' => ''
);
}

add_filter('post_updated_messages', 'post_type_updated_messages');
function post_type_updated_messages( $messages ) {
global $post, $post_ID;

$messages['download'] = array(
0 => '', // Unused. Messages start at index 1.
1 => sprintf( __('Download updated. <a href="%s">View Download</a>'), esc_url( get_permalink($post_ID) ) ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Download updated.'),
/* translators: %s: date and time of the revision */
5 => isset($_GET['revision']) ? sprintf( __('Download restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Download published. <a href="%s">View Download</a>'), esc_url( get_permalink($post_ID) ) ),
7 => __('Download saved.'),
8 => sprintf( __('Download submitted. <a target="_blank" href="%s">Preview Download</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __('Download scheduled for: %1$s. <a target="_blank" href="%2$s">Preview Download</a>'),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __('Download draft updated. <a target="_blank" href="%s">Preview Download</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
return $messages;
}

// Download New Icons Hack
add_action('admin_head', 'plugin_header');
function plugin_header() {
global $post_type;
?>
<style>
<?php if (($_GET['post_type'] == 'download') || ($post_type == 'download')) : ?>
#icon-edit { background: transparent url('<?php echo get_bloginfo('url');?>/wp-content/plugins/download-monitor/img/downloads-icon.gif') no-repeat -5px -4px; }
<?php endif; ?>
</style>
<?php
}

// Downloads Upload Directory
add_filter( 'upload_dir', 'my_custom_upload_dir' );
function my_custom_upload_dir( $default_dir ) {

if ( ! isset( $_POST['post_id'] ) || $_POST['post_id'] < 0 )
return $default_dir;
if ( get_post_type( $_POST['post_id'] ) != 'download' )
return $default_dir;

$dir = WP_CONTENT_DIR . '/downloads';
$url = WP_CONTENT_URL . '/downloads';

$bdir = $dir;
$burl = $url;
$subdir = '';

if ( get_option( 'uploads_use_yearmonth_folders' ) ) {

$time = current_time( 'mysql' );
$y = substr( $time, 0, 4 );
$m = substr( $time, 5, 2 );
$subdir = "/$y/$m";
}

$dir .= $subdir;
$url .= $subdir;

$custom_dir = array(

'path' => $dir,
'url' => $url,
'subdir' => $subdir,
'basedir' => $bdir,
'baseurl' => $burl,
'error' => false,
);

return $custom_dir;
}

// Downloads Taxonomies
add_action( 'init', 'create_download_taxonomies', 0 );
function create_download_taxonomies()
{
// Hierarchical Like Categories
$labels = array(
'name' => _x( 'Grouping', 'taxonomy general name' ),
'singular_name' => _x( 'Group', 'taxonomy singular name' ),
'search_items' => __( 'Search Groups' ),
'all_items' => __( 'All Groups' ),
'parent_item' => __( 'Parent Group' ),
'parent_item_colon' => __( 'Parent Group:' ),
'edit_item' => __( 'Edit Group' ),
'update_item' => __( 'Update Group' ),
'add_new_item' => __( 'Add New Group' ),
'new_item_name' => __( 'New Group Name' ),
'menu_name' => __( 'Group' )
);

register_taxonomy('group',array('download'), array(
'hierarchical' => true,
'label' => 'Groups',
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'public'=>true,
'rewrite' => array( 'slug' => 'downloads/group' )
));

// Non Hierarchical Like Tags
$labels = array(
'name' => _x( 'Meta', 'taxonomy general name' ),
'singular_name' => _x( 'Meta', 'taxonomy singular name' ),
'search_items' => __( 'Search Meta' ),
'popular_items' => __( 'Popular Meta' ),
'all_items' => __( 'All Meta' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Meta' ),
'update_item' => __( 'Update Meta' ),
'add_new_item' => __( 'Add New Meta' ),
'new_item_name' => __( 'New Meta Name' ),
'separate_items_with_commas' => __( 'Separate meta with commas' ),
'add_or_remove_items' => __( 'Add or remove meta' ),
'choose_from_most_used' => __( 'Choose from the most used meta' ),
'menu_name' => __( 'Meta' )
);

register_taxonomy('meta','download',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'downloads/meta' )
));
}

// Downloads Post Type Filter Drop Down & Columns
function restrict_download_by_group() {
global $typenow;
$post_type = 'download'; // change HERE
$taxonomy = 'group'; // change HERE
if ($typenow == $post_type) {
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
$info_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Show All {$info_taxonomy->label}"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => true,
));
};
}

add_action('restrict_manage_posts', 'restrict_download_by_group');

function convert_id_to_term_in_query($query) {
global $pagenow;
$post_type = 'download'; // change HERE
$taxonomy = 'group'; // change HERE
$q_vars = &$query->query_vars;
if ($pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0) {
$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
$q_vars[$taxonomy] = $term->slug;
}
}

add_filter('parse_query', 'convert_id_to_term_in_query');

This question has been answered.

Josh Cranwell | 01/05/12 at 9:56am Edit

Previous versions of this question: 01/05/12 at 10:03am | 01/05/12 at 1:11pm | 01/05/12 at 1:13pm | 01/05/12 at 1:14pm | 01/05/12 at 1:15pm | 01/05/12 at 1:15pm

(4) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    01/05/12
    10:19am
    Julio Potier says:

    Hello

    Can you paste the link of an incorrect picture here ?

    Right clic on the missing picture in the browser > "copy image location" (kind of)

    Thank you

    • 01/05/12 12:02pm

      Josh Cranwell says:

      It's looking in the wp-content/uploads/2012/1 folder which is wrong :-/

      It should be looking in wp-content/downloads/2012/1

      Thanks

  • avatar
    Last edited:
    01/05/12
    10:34am
    Kannan C says:

    You are uploading the images to a custom upload folder, right? I think, You need to update the attachment meta data to reflect your new uploads folder path.

    • 01/05/12 12:05pm

      Josh Cranwell says:

      Hmmmm. Is this the same thing as below?

  • avatar
    Last edited:
    01/05/12
    11:30am
    Peter Michael says:

    The variable

    $_POST['post_id']
    inside your filter function
    my_custom_upload_dir
    is undefined, hence it always returns
    $default_dir
    . You need to find another way to get the current post type and apply the filter.

    • 01/05/12 12:04pm

      Josh Cranwell says:

      You are right but it's beyond me, do you how to change this? Thanks

  • avatar
    Last edited:
    01/05/12
    2:57pm
    Manoj Raj says:

    Here is one simple answer

    Add this to your functions.php

    function custom_upload_directory( $args ) {

    $id = $_REQUEST['post_id'];
    $parent = get_post( $id )->post_parent;

    // Check the post-type of the current post
    if( "download" == get_post_type( $id ) || "download" == get_post_type( $parent ) ) {
    $args['path'] = "/home2/user/public_html/example/wpquestions/wp-content/downloads";
    $args['url'] = "http://www.example.com/wpquestions/wp-content/downloads";
    $args['basedir'] = "/home2/user/public_html/example/wpquestions/wp-content/downloads";
    $args['baseurl'] = "http://www.example.com/wpquestions/wp-content/downloads";
    }
    return $args;
    }
    add_filter( 'upload_dir', 'custom_upload_directory' );


    Change the path and url accordingly

    Previous versions of this answer: 01/05/12 at 2:57pm

    • 01/05/12 1:40pm

      Josh Cranwell says:

      Hi Monoj, is your above code not doing the same as this...?


      // Downloads Upload Directory
      add_filter( 'upload_dir', 'my_custom_upload_dir' );
      function my_custom_upload_dir( $default_dir ) {

      if ( ! isset( $_POST['post_id'] ) || $_POST['post_id'] < 0 )
      return $default_dir;
      if ( get_post_type( $_POST['post_id'] ) != 'download' )
      return $default_dir;

      $dir = WP_CONTENT_DIR . '/downloads';
      $url = WP_CONTENT_URL . '/downloads';

      $bdir = $dir;
      $burl = $url;
      $subdir = '';

      if ( get_option( 'uploads_use_yearmonth_folders' ) ) {

      $time = current_time( 'mysql' );
      $y = substr( $time, 0, 4 );
      $m = substr( $time, 5, 2 );
      $subdir = "/$y/$m";

      }

      $dir .= $subdir;
      $url .= $subdir;

      $custom_dir = array(

      'path' => $dir,
      'url' => $url,
      'subdir' => $subdir,
      'basedir' => $bdir,
      'baseurl' => $burl,
      'error' => false,

      );

      return $custom_dir;

      }


      I've already got the custom directory in place, I need the attachment meta data to be changes as well. Thanks

    • 01/05/12 1:42pm

      Manoj Raj says:

      hi i have it working fine.. Thumbnail is working fine and the link url is also working fine for me..

      Replace the exisiting code with this.. Do the necessary changes in path and url..

    • 01/05/12 1:50pm

      Josh Cranwell says:

      Ahh I see, OK so my above code is wrong some how.

      I don't suppose you know how to fix it, because it's technically doing the same thing right but with an error somewhere. Plus it would be cleaner as I don't need to add any URLS.

      Thanks

    • 01/05/12 1:55pm

      Manoj Raj says:

      Sorry i confused you with the urls...

      Here is the revised code.


      function custom_upload_directory( $args ) {



      $id = $_REQUEST['post_id'];

      $parent = get_post( $id )->post_parent;



      // Check the post-type of the current post

      if( "download" == get_post_type( $id ) || "download" == get_post_type( $parent ) ) {

      $args['path'] = WP_CONTENT_DIR .'/downloads';
      $args['url'] = WP_CONTENT_URL .'/downloads';
      $args['basedir'] = WP_CONTENT_DIR .'/downloads';
      $args['baseurl'] = WP_CONTENT_URL .'/downloads';

      }

      return $args;

      }

      add_filter( 'upload_dir', 'custom_upload_directory' );



      Just paste this code and check.. It will work for sure..

    • 01/05/12 2:08pm

      Manoj Raj says:

      If you want to use year/month organized here is the code


      function custom_upload_directory( $args ) {

      $id = $_REQUEST['post_id'];
      $parent = get_post( $id )->post_parent;

      // Check the post-type of the current post

      if( "download" == get_post_type( $id ) || "download" == get_post_type( $parent ) ) {

      $args['path'] = WP_CONTENT_DIR .'/downloads'. $args['subdir'];
      $args['url'] = WP_CONTENT_URL .'/downloads'. $args['subdir'];
      $args['basedir'] = WP_CONTENT_DIR .'/downloads';
      $args['baseurl'] = WP_CONTENT_URL .'/downloads';
      }
      return $args;
      }

      add_filter( 'upload_dir', 'custom_upload_directory' );

    • 01/06/12 7:05am

      Josh Cranwell says:

      Hi Manoj,

      I seem to run into problems with this idea. Not your code as your code works perfect and better than my previous code. I've been testing all aspects to make sure I'm not missing anything.

      The problem is that they are not in the media library. This is obvious change I understand, but how did manage your images when adding images to posts?

      For example, in my custom post type, if I go 'add images', and I want to add from previous pictures uploaded, normally you go to media library and add previous images. Is it possible to create another media library just for my custom upload directory? Custom Media Library?

      Thanks.

This question has expired.



Gabriel Reguly, Josh Cranwell, Francisco Javier Carazo Gil voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.