mercredi 25 février 2015

Customize WordPress Upload Directory


I'm trying upload certain files to the following custom director:



/wp-content/uploads/audio/yyyy/mm


However, the year and month directories are not being created properly. They are showing up like this:



/wp-content/uploads/audio/%25year%25/%25month%25/


Here is the code that I'm trying to make this work with.



function custom_upload_directory( $path ) {

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

// Check the post-type of the current post
if( "risen_multimedia" == get_post_type( $id ) || "risen_multimedia" == get_post_type( $parent ) ) {

$customdir = "/audio";

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

$path['path'] = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month)
$path['url'] = str_replace($path['subdir'], '', $path['url']);
$path['subdir'] = $customdir . $subdirectory;
$path['path'] .= $path['basedir'] . $customdir . $subdirectory;
$path['url'] .= $path['baseurl'] . $customdir . $subdirectory;
return $path;
}
return $path;
}
add_filter( 'upload_dir', 'custom_upload_directory' );


So I'm not sure why the year and month are showing up as words with % signs around them instead of integers?





Aucun commentaire:

Enregistrer un commentaire