I have found a way to add a taxonomy or category as a folder list to the media edit screen so you can create and select folders by ticking boxes rather than entering folder changes manually into an input box. It works perfectly except for one little hitch – you have to update the media item twice! First to get the taxonomy folder list changes updated so then they can be used by the plugin to do its stuff. My skills in WordPress have now reached there limit. Any ideas?
The following changes were made in plugins\wp-media-folders\classes\classes\wp-media-folders.php Line 140-173 are replaced with:
/**
* ###############################################################################
* Add hierarchical taxonomy called ‘Folders’ to ‘Media’ menu
* https://code.tutsplus.com/articles/applying-categories-tags-and-custom-taxonomies-to-media-attachments–wp-32319
* register new taxonomy which applies to attachments
*/
function wptp_add_folders_taxonomy() {
$labels = array(
‘name’ => ‘Folders’,
‘singular_name’ => ‘Folder’,
‘search_items’ => ‘Search Folders’,
‘all_items’ => ‘All Folders’,
‘parent_item’ => ‘Parent Folder’,
‘parent_item_colon’ => ‘Parent Folder:’,
‘edit_item’ => ‘Edit Folder’,
‘update_item’ => ‘Update Folder’,
‘add_new_item’ => ‘Add New Folder’,
‘new_item_name’ => ‘New Folder Name’,
‘menu_name’ => ‘Folders’,
);
$args = array(
‘labels’ => $labels,
‘hierarchical’ => true,
‘query_var’ => ‘true’,
‘rewrite’ => ‘true’,
‘show_admin_column’ => ‘true’,
);
register_taxonomy( ‘folders’, ‘attachment’, $args );
}
add_action( ‘init’, ‘wptp_add_folders_taxonomy’ );
/**
* Change file path using the taxonomy of folders.
* BUT this requires pressing [Update] twice as have to save taxonomy/folder changes first.
*/
add_filter(
‘attachment_fields_to_edit’,
function ($form_fields, $post) {
$url = wp_get_attachment_url($post->ID);
$uploads = wp_upload_dir();
if (strpos($url, $uploads[‘baseurl’])!==0) {
$html = __(‘This file is not in the allowed upload folder’, ‘wp-media-folders’);
} else {
$path = str_replace($uploads[‘baseurl’], “”, $url);
$file_extension = pathinfo($path, PATHINFO_EXTENSION);
$path = ‘/’.$post->post_name;
$folders_array = wp_get_post_terms($post->ID, ‘folders’, array(‘orderby’ => ‘parent’, “fields” => “names”));
if (!empty($folders_array)) {$path = ‘/’.strtolower(implode(“/”,$folders_array)).$path;}
//$html = ‘<span style=”width:80%;” name=”attachments[‘.$post->ID.’][file_path]” id=”attachments[‘.$post->ID.’][file_path]” value=”‘.htmlentities($path).'”>’.htmlentities($path).'</span>’.’.’.$file_extension;
$html = ‘<input style=”width:80%;” name=”attachments[‘.$post->ID.’][file_path]” id=”attachments[‘.$post->ID.’][file_path]” value=”‘.htmlentities($path).'” /> . ‘.$file_extension;
}
$form_fields[‘file_path’] = array(
‘label’ => __(‘File path’, ‘wp-media-folders’),
‘input’ => ‘html’,
‘html’ => $html,
‘helps’ => __(sprintf(‘File path and name related to upload folder %s’, ‘/’ . substr($uploads[‘basedir’], strlen(get_home_path()))), ‘wp-media-folders’)
);
return $form_fields;
},
10,
2
);
/**
* #####################################################################
*/