setting image title/caption
-
i see from https://ww.wp.xz.cn/support/topic/store-image-caption/ and https://ww.wp.xz.cn/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?
-
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
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"]; }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
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…
btw, the code u gave is the same as that in https://ww.wp.xz.cn/support/topic/store-image-caption/ and it still doesn’t work:-{
The topic ‘setting image title/caption’ is closed to new replies.