Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom post type upload media new location WordPress

  • SOLVED

I've changed my question as this may be a better option - and it's less vague

Though for reference find my original question here if you want to read. [[LINK href="http://sharetext.org/CKMQ"]]Plain Text[[/LINK]] or [[LINK href="http://sharetext.org/CKMT"]]HTML[[/LINK]]

<strong>New Question</strong>

I've created a post type called downloads using the functions below...


// 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,
'menu_icon' => 'http://www.repsolhondateam.com/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: <strong>%1$s</strong>. <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;
}


...seems to work ok.

But is it possible to change the location of the uploads folder just for this post type only?

For example lets say a 'downloads' folder in the wp-content folder. Keeping the normal 'uploads' folder for page/post uploads.

Can this easily be done? So it also automatically puts the correct image link into the post when adding images? Instead of the 'uploads' folder link?

Thanks

Answers (4)

2011-12-13

Gabriel Reguly answers:

Hi,

Do you have a time frame for the task?

I can do it, but not this week.

Also would be nice to know if you have a budget in mind.

Regards,
Gabriel


Josh Cranwell comments:

I'm working on this day and night for this Thursday, so asap. Thanks anyway


Gabriel Reguly comments:

Ok, good luck for you then.


Gabriel Reguly comments:

Hi,

Please try this code:



add_filter( 'upload_dir', 'my_custom_upload_dir' );

function my_custom_upload_dir( $default_dir ){
/*
* On success, the returned array will have many indices:
* 'path' - base directory and sub directory or full path to upload directory.
* 'url' - base url and sub directory or absolute URL to upload directory.
* 'subdir' - sub directory if uploads use year/month folders option is on.
* 'basedir' - path without subdir.
* 'baseurl' - URL path without subdir.
* 'error' - set to false.
*/


// Adjust your settings here
$dir = '';
$url = '';
$subdir = '';
$bdir = '';
$burl = '';
$custom_dir = array(
'path' => $dir,
'url' => $url,
'subdir' => $subdir,
'basedir' => $bdir,
'baseurl' => $burl );
return shortcode_atts( $custom_dir, $default_dir );
}


Regards,
Gabriel


Gabriel Reguly comments:

Remember to check for your post type,

if ( $post->post_type == 'download' )

before adjusting your new directory


Josh Cranwell comments:

This looks really good. Ok so if I want to put my 'downloads' folder in this location..

get_bloginfo('url');/wp-content/downloads

How the best way to add it below?


add_filter( 'upload_dir', 'my_custom_upload_dir' );

function my_custom_upload_dir( $default_dir ){

$dir = '';

$url = '';

$subdir = '';

$bdir = '';

$burl = '';

$custom_dir = array(

'path' => $dir,

'url' => $url,

'subdir' => $subdir,

'basedir' => $bdir,

'baseurl' => $burl );

return shortcode_atts( $custom_dir, $default_dir );

}


Thanks,
Josh


Josh Cranwell comments:

Actually does this change everything globally?

I'm not very good at this stuff unless I'm reading it from a tutorial, but can't find much on this.

In my functions.php, how do I write the above to work?

This is what I've got below, but I need to include <strong>get_bloginfo('url');</strong> so that it uses my current permalink settings.

And the location of the new folder needs to be 'wp-content/downloads/

And the upload structure is fine using the default dated folders '2011/12'

Really appreciate your help thanks.

if ( $post->post_type == 'download' ) {

add_filter( 'upload_dir', 'my_custom_upload_dir' );
function my_custom_upload_dir( $default_dir ){

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

$custom_dir = array(
'path' => $dir,
'url' => $url,
'subdir' => $subdir,
'basedir' => $bdir,
'baseurl' => $burl );

return shortcode_atts( $custom_dir, $default_dir );

}
}


Gabriel Reguly comments:

Hi Josh,

Sorry, but I do not have the time to test the code.
You probably will need to amend it, regarding the correct path/dir values.
Please add the following to your functions.php file

add_filter( 'upload_dir', 'my_custom_upload_dir' );
function my_custom_upload_dir( $default_dir ){
global $post;
if ( $post->post_type != 'download' ) {
return $default_dir;
}

/*
* On success, the returned array will have many indices:
* 'path' - base directory and sub directory or full path to upload directory.
* 'url' - base url and sub directory or absolute URL to upload directory.
* 'subdir' - sub directory if uploads use year/month folders option is on.
* 'basedir' - path without subdir.
* 'baseurl' - URL path without subdir.
* 'error' - set to false.
*/

// Adjust your settings here
$bdir = 'wp-contents';
$subdir = date( 'Y/m' );
$dir = $bdir . $subdir;

$burl = content_url('downloads');
$url = $burl . $subdir;


$custom_dir = array(
'path' => $dir,
'url' => $url,
'subdir' => $subdir,
'basedir' => $bdir,
'baseurl' => $burl );

return shortcode_atts( $custom_dir, $default_dir );
}



Regards,
Gabriel


Josh Cranwell comments:

Thanks man this is looking good. I'm just leaving office to carry on at home so I'll let you know how I get on. Thanks for your time.


Josh Cranwell comments:

I've tried your code and tested an upload in my custom post 'download'

The uploads for this post type are still going to the uploads folder

See my directory stucture [[LINK href="http://stackoverflow.com/questions/8519968/changing-location-of-uploads-folder-for-custom-post-type-only-not-working"]]here[[/LINK]]

I don't understand why it's not working, i've added some more money for you.


Gabriel Reguly comments:

Hi Josh,

Maybe tomorrow I can help you.

Would you mind giving me access to your site? Please send me a PM with login details if possible.

Regards,
Gabriel

2011-12-14

Julio Potier answers:

Hello

i just tried to mass upload with "download monitor" and this is possible with the "add directory" menu.
This option page will add all files matching a file extension pattern.
Is it correct fir you ?

ps: for the BMP thumbnail i think this hack is doable.


Josh Cranwell comments:

Thanks for you answer, I tried it but It seem like to much hassle to track these downloads. So I've update my question above for a new idea. I'll vote you anyway thanks.

2011-12-15

Manoj Raj answers:

How about this plugin "Custom Upload Dir" by ulfben?... Have you tried this one?

http://wordpress.org/extend/plugins/custom-upload-dir/


Josh Cranwell comments:

Hello again Manoj

I've had a look but it only allows you to do it overall

I need this work for only my custom post type 'download' but remain the same for page and post uploads.

Tricky huh?

2011-12-16

Matt Varone answers:

Hi again Josh :)

Gabriel solution is the most appropriate given you adjust some of it's code:

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;
}


This should get your files inside the downloads folder for the download CPT. Remember to create the directory on wp-content.

Kind Regards!


Josh Cranwell comments:

Worked an absolute beauty!!!!

Again thanks Matt Varone for your precise reply - I may get back to you with a similar question after new year with refining the directory sorting.

Thanks very much! Speak soon!