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 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 wrote a plugin to register members 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' );