I'm working with modifying the upload directory depending on which post type is being uploaded to. I've ran across this question: Post type specific upload folder in 3.5 and one of the lines confused me:

$uploads = apply_filters("{$type}_upload_directory", $type);

Where $type is the post type. They then go on to join the paths:

$dir['path'] = path_join($dir['basedir'], $uploads);
$dir['url'] = path_join($dir['baseurl'], $uploads);

Which works fine, you get the expected upload folder of wp-content/uploads/post-type-name but what does the apply_filtes achieve? It seems like I could just change that line to:

$uploads = $type or $dir['path'] = path_join($dir['basedir'], $type);

and I will still get the same folder structure, so what does the apply_fitlers portion actually achieve? I can't seem to find reference on the filter anywhere either.


'apply_filters' runs the value through all of the other hooks / functions attached to it. Try an experiment:

print_r( $type );
print_r( apply_filters("{$type}_upload_directory", $type) );

While you might, in your case, get the same thing - the apply_filters makes sure that any other plugins that try to influence this upload directory will still work.

  • Doing some research it doesn't even seem like there is a filter for "{posttype}_upload_directory" or a filter called "upload_directory" all I can find is "upload_dir" filter which leads me to believe that the filter is not needed. Do you have any links where the above filters exist in WordPress?
    – Howdy_McGee
    Nov 11 '14 at 22:08
  • 1
    The filter is not needed, it's just added by the author for convenience to allow you to modify it later on from another plugin.
    – birgire
    Nov 12 '14 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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