Support » Plugin: Wordpress File Upload » How to provide post_title, post_content. post_excerpt

  • I have configured “Add Uploaded Files To Media” and would like to also provide the title, description, caption, and set a category or tag value when the image is uploaded. Can this be done?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author nickboss

    (@nickboss)

    Hi, yes it can be done however it requires custom code and implementation of some plugin’s hooks. I can help you with this. Do you have these attributes (title, description etc.) as additional fields in the upload form?

    Regards

    Nickolas

    Thread Starter sgb02

    (@sgb02)

    Yes I defined the following fields:
    Title
    Caption
    Description
    Photo Date
    Categories
    Tags (separate with space)

    Can you show how to map to the standard image fields, categories, tags?

    Plugin Author nickboss

    (@nickboss)

    Hi, I have created a hook for you that adds title, caption and description to the image based on the fields, but I have some questions:

    a. Photo Date: you mean the upload date or something else?
    b. Categories and Tags. The default WordPress Media Library does not provide categories and tags. Are you using any plugin for setting them? If not, we can configure the plugin to create them.

    Nickolas

    Thread Starter sgb02

    (@sgb02)

    Thanks for working on this… 🙂

    a) Photo date – the date the photo was taken
    b) yes I have been experimenting with a plugin (MLA) that adds the standard post categories and tags, or custom taxonomies just for images. I think both approaches have merit, but leaning toward the custom taxonomy approach.

    Plugin Author nickboss

    (@nickboss)

    a) Regarding the Photo date, there is no default field in an attachment for when a photo was taken. So, is this going to be a custom field?

    Nickolas

    Thread Starter sgb02

    (@sgb02)

    Yes I changed the “Photo Date” field to be called “Created” and will save as a custom field.

    Plugin Author nickboss

    (@nickboss)

    Ok here is an initial hook that will add title, caption, description and created date.

    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 ) ) );
    		$caption = ( isset($userdata_fields[1]) ? $userdata_fields[1]["value"] : "" );
    		$description = ( isset($userdata_fields[2]) ? $userdata_fields[2]["value"] : "" );
    		$created = ( isset($userdata_fields[3]) ? $userdata_fields[3]["value"] : "" );
    		$attachment = array(
    			'guid'           => $wp_upload_dir['url'] . '/' . wfu_basename( $file_path ), 
    			'post_mime_type' => $filetype['type'],
    			'post_title'     => $title,
    			'post_content'   => $description,
    			'post_excerpt'   => $caption,
    			'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 );
    		$attach_data["WFU User Data"]["Created"] = $created;
    		$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";
    }

    You need to add it at the end of functions.php file of your theme.

    It is left for me to add category and tag taxonomies. Let me know that we are ok so far.

    Nickolas

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to provide post_title, post_content. post_excerpt’ is closed to new replies.