Title: setting image title/caption
Last modified: November 11, 2022

---

# setting image title/caption

 *  [airdrummer](https://wordpress.org/support/users/airdrummer/)
 * (@airdrummer)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/setting-image-title-caption/)
 * i see from [https://wordpress.org/support/topic/store-image-caption/](https://wordpress.org/support/topic/store-image-caption/)
   and [https://wordpress.org/support/topic/how-to-provide-post_title-post_content-post_excerpt/](https://wordpress.org/support/topic/how-to-provide-post_title-post_content-post_excerpt/)
   how to do this, but i’d like to use your wfu_after_upload hook.
 * is the attachment page id available in the globals? or via get_the_ID?

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Plugin Author [nickboss](https://wordpress.org/support/users/nickboss/)
 * (@nickboss)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/setting-image-title-caption/#post-16208178)
 * Hi, I suppose that you are adding the uploaded files in Media Library (as attachments)
   and you want to set the title and caption of the attachment.
 * Here is a hook that does this, not by using wfu_after_upload hook, but by subclassing
   wfu_process_media_insert() function.
 *     ```
       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";
       }
       ```
   
 * Best Regards
 * Nickolas
 *  Thread Starter [airdrummer](https://wordpress.org/support/users/airdrummer/)
 * (@airdrummer)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/setting-image-title-caption/#post-16208838)
 * ok, thanx…where are these userdata_fields defined?
 *     ```
       $title = $userdata_fields[0]...
       $description = $userdata_fields[2]...
       $categories = $userdata_fields[1]...
       ```
   
 * my userdata field is named “location description”
 *     ```
       <div id="userdata_2_0" class="file_userdata_container" style="">
           <style>#userdata_2_label_0:after { content: '(required)'; }</style>
           <label id="userdata_2_label_0" for="userdata_2_field_0" class="file_userdata_label" style="width: unset; ">Location description</label>
           <div id="userdata_2_fieldwrapper_0" class="file_userdata_fieldwrapper_required" style="width: 95%; ">
               <div class="wfu_fieldwrapper_overlay" onclick="document.getElementById('userdata_2_field_0').focus();"></div>
               <!-- **** the following lines contain the HTML code of each field type ***** -->
               <input type="text" id="userdata_2_field_0" class="file_userdata_message" value="" autocomplete="off" form="dummy_2" onfocus="GlobalData.WFU[2].userdata._focused(this);">
               <input id="userdata_2_props_0" type="hidden" value="p:inline">
               <!-- ***************** end of HTML code of each field type ***************** -->
           </div>
           <!-- the hint element -->
           <div id="userdata_2_hint_0" class="file_userdata_hint" style="display:none;" onclick="document.getElementById('userdata_2_field_0').focus();">
                   empty
           </div>
       </div>
       ```
   
 * so should the userdata_fields be searched?
 *     ```
       foreach ( $userdata_fields as $userdata_key => $userdata_field )
        {
           if (isset($userdata_field)
           && ("Location description" == $userdata_field['label']))
               $title = $userdata_field["value"];
        }
       ```
   
 *  Thread Starter [airdrummer](https://wordpress.org/support/users/airdrummer/)
 * (@airdrummer)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/setting-image-title-caption/#post-16209362)
 * unfortunately, updating the attachment seems to have reset the attachment order,
   so that a new image gets put @ the beginning of the the list, rather than the
   end…i’ll see if there’s a way to sort get_attached_media by date/id
 *  Thread Starter [airdrummer](https://wordpress.org/support/users/airdrummer/)
 * (@airdrummer)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/setting-image-title-caption/#post-16210094)
 *     ```
       function sortWPPObyID( $a,  $b)
       {
           return (int)$a->ID <=> (int)$b->ID;
       }
       ...
       	$attachments = get_attached_media( 'image', $post->ID );
       	usort($attachments, "sortWPPObyID");
       ```
   
 * but i’m still not getting my userdata field…
 *  Thread Starter [airdrummer](https://wordpress.org/support/users/airdrummer/)
 * (@airdrummer)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/setting-image-title-caption/#post-16212338)
 * btw, the code u gave is the same as that in [https://wordpress.org/support/topic/store-image-caption/](https://wordpress.org/support/topic/store-image-caption/)
   and it still doesn’t work:-{

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘setting image title/caption’ is closed to new replies.

 * ![](https://ps.w.org/wp-file-upload/assets/icon-256x256.png?rev=3252590)
 * [Iptanus File Upload](https://wordpress.org/plugins/wp-file-upload/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-file-upload/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-file-upload/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-file-upload/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-file-upload/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-file-upload/reviews/)

 * 5 replies
 * 2 participants
 * Last reply from: [airdrummer](https://wordpress.org/support/users/airdrummer/)
 * Last activity: [3 years, 6 months ago](https://wordpress.org/support/topic/setting-image-title-caption/#post-16212338)
 * Status: not resolved