Adding Categories to Media Library Images
-
Hi,
I’m trying to add files automatically to the Media Library, including categories using the former post : https://www.ads-software.com/support/topic/adding-categories-to-media-library-images/However categories are not added to my uploaded files in the Media Library and I don’t understand why.
My form is quite simple (for now…) :
0- Title (text)
1- Type (listbox with 3 types ‘actualite’, ‘article’, ‘orale’) (=> which I want to be category in the Media Library)
3- Caption (text)With the following code, title and caption are well included in the Media Library but uploaded files have no category.
I’m also using Enhanced Media Library and I have already created the three categories in the Media Library : ‘actualite’, ‘article’, ‘orale’if ( isset($GLOBALS["WFU_GLOBALS"]["WFU_DEBUG"]) ) $GLOBALS["WFU_GLOBALS"]["WFU_DEBUG"][3] = "ON"; if (!function_exists('wfu_debug_wfu_process_media_insert_function_handler')) { function wfu_debug_wfu_process_media_insert_function_handler($res, $file_path, $userdata_fields, $page_id) { $wp_upload_dir = wp_upload_dir(); $filetype = wp_check_filetype( wfu_basename( $file_path ), null ); $title = ( isset($userdata_fields[0]) && trim($userdata_fields[0]["value"]) != "" ? trim($userdata_fields[0]["value"]) : preg_replace( '/\.[^.]+$/', '', wfu_basename( $file_path ) ) ); $description = ( isset($userdata_fields[2]) ? $userdata_fields[2]["value"] : "" ); $attachment = array( 'guid' => $wp_upload_dir['url'] . '/' . wfu_basename( $file_path ), 'post_mime_type' => $filetype['type'], 'post_title' => $title, 'post_content' => $description, 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $file_path, $page_id ); require_once(ABSPATH . 'wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata( $attach_id, $file_path ); $update_attach = wp_update_attachment_metadata( $attach_id, $attach_data ); $filedata = wfu_get_filedata($file_path, true); if ( $filedata != null ) { $filedata["media"] = array( "type" => "data", "attach_id" => $attach_id ); wfu_save_filedata_from_id($filedata["general"]["idlog"], $filedata); } $res["result"] = 'R'; $res["output"] = $attach_id; return $res; } add_filter('wfu_debug-wfu_process_media_insert', 'wfu_debug_wfu_process_media_insert_function_handler', 10, 4); $GLOBALS['wfu_debug-wfu_process_media_insert'] = "1"; } if (!function_exists('wfu_file_upload_output_custom_handler')) { function wfu_file_upload_output_custom_handler($output, $params) { $sid = $params["uploadid"]; $fid = 1; $categories = array( "actualite", "article", "orale" ); $output .= "\n".'<input type="hidden" id="wfu_categories_'.$sid.'" value="'.implode(",", $categories).'" />'."\n"; $output .= " <script type=\"text/javascript\"> if(window.addEventListener) { window.addEventListener(\"load\", wfu_tweak_listbox, false); } else if(window.attachEvent) { window.attachEvent(\"onload\", wfu_tweak_listbox); } else { window[\"onload\"] = wfu_tweak_listbox; } var wfu_tweak_listbox = function() { var WFU = GlobalData.WFU[$sid]; if (!WFU.userdata._getValue) WFU.userdata._getValue = WFU.userdata.getValue; WFU.userdata.getValue = function(props) { var field = document.getElementById('userdata_' + $sid + '_field_' + props.key); var value = ''; if (props.type == 'list') { for (var i = 0; i < field.options.length; i++) if (field.options[i].selected) value += (value == '' ? '' : ',') + field.options[i].value; } else value = this._getValue(props); return value; } var categories = document.getElementById('wfu_categories_' + $sid).value.split(','); var findex = $fid - 1; var list = document.getElementById('userdata_' + $sid + '_field_' + findex); while (list.options.length > 0) list.options.remove(0); for (var i = 0; i < categories.length; i++) { var opt = document.createElement('OPTION'); opt.value = categories[i]; opt.innerHTML = categories[i]; list.options.add(opt); } list.selectedIndex = -1; WFU.userdata.props[findex].store(); }(); </script> "; return $output; } add_filter('_wfu_file_upload_output', 'wfu_file_upload_output_custom_handler', 10, 2); }
Are you able to find where i’m wrong in this code ?
Regards.
Maxime
- The topic ‘Adding Categories to Media Library Images’ is closed to new replies.