I read this post and found it really interesting. I tried to modify it a little to make it work if you have a custom db table and not really a post type to rely on. So I tried to figure that out using get_current_screen() instead of get_post_type().
In this example I did a little plugin to register members of some kind (just for the idea of using this in any kind of plugin that doesn’t use post types) and have the profile photos upload to the plugin dir just like in the original code. Although this doesnât seem to work, the files are still saved in the regular “wp-content/uploads/” dir. Any ideas?
The plugin itself uses a simple form to save data to db and I also hooked the wp-uploader to upload and send the image url with the rest of the data.
function custom_upload_directory( $args ) {
$screen = get_current_screen();
// If the parent_base is members, upload to plugin directory
if ( $screen->parent_base == âmembersâ ) {
$args['path'] = plugin_dir_path(__FILE__) . âuploadsâ;
$args['url'] = plugin_dir_url(__FILE__) . âuploadsâ;
$args['basedir'] = plugin_dir_path(__FILE__) . âuploadsâ;
$args['baseurl'] = plugin_dir_url(__FILE__) . âuploadsâ;
}
return $args;
}
add_filter( âupload_dirâ, âcustom_upload_directoryâ );
Typo: ‘ not is â
You have a weired Editor. You’re simply using the wrong type of quotes:
â
should be"
â
should be'
Here’s the changed function:
Debug
Normally that should throw an error. Be sure that you have set
WP_DEBUG
set toTRUE
inside yourwp-config.php
file when testing code inside a development environment.On production sites you could also enable the error log (don’t do this constantly and disable error display so errors are gracefully logged and not displayed to your users.
See the editing wp-config page for more info on defining configuration constants.