• 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

    • This topic was modified 5 years, 9 months ago by mleroy4.
    • This topic was modified 5 years, 9 months ago by mleroy4.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author nickboss

    (@nickboss)

    Hi function wfu_debug_wfu_process_media_insert_function_handler() does not include the code to add the categories inside the attachment, that is why you do not see it.

    Here is an improved version (I haven’t tested it though):

    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) {
    		$attach_id = $res["output"];
    		if ( $attach_id ) {
    			$title = ( isset($userdata_fields[0]) ? $userdata_fields[0]["value"] : "" );
    			$description = ( isset($userdata_fields[2]) ? $userdata_fields[2]["value"] : "" );
    			$args = array( 'ID' => $attach_id, 'post_title' => $title, 'post_content' => $description );
    			wp_update_post( $args );
    			$categories = ( isset($userdata_fields[1]) ? explode(",", $userdata_fields[1]["value"]) : array() );
    			foreach ( $categories as $slug ) {
    				$cat = get_category_by_slug( $slug );
    				if ( $cat !== false ) wp_set_object_terms( $attach_id, $slug, 'category', true );
    			}
    		}
    		$res["result"] = 'R';
    		return $res;
    	}
    	add_filter('wfu_debug-wfu_process_media_insert', 'wfu_debug_wfu_process_media_insert_function_handler', 10, 4);
    	$GLOBALS['wfu_debug_end-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);
    }

    From what I see, you have customized the listbox to be multiselect, right?

    Regards

    Nickolas

    Thread Starter mleroy4

    (@mleroy4)

    Thanks for your reply.

    It still does not works. catégories are not added in the library
    And I’m using the basic listbox in my form (which seems to be multiselect, no ?). Actually I’d like to use a checkbox instead. But for now I’ve only “copy-paste” the code to try to make it works.
    I don’t know what informations I can give you to help you help me ??

    By the way thank you very much for this plugin !

    Regards

    And sorry if I make mistakes when I’m writtig, I’m french…

    Plugin Author nickboss

    (@nickboss)

    What is the URL of the page with the plugin to have a look?

    Nickolas

    Thread Starter mleroy4

    (@mleroy4)

    I’m working in local for the moment.
    I will put it online probably tommorow (There is a delay to get a free website host)

    Thank you !

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding Categories to Media Library Images’ is closed to new replies.