• Resolved justmigrating

    (@justmigrating)


    Hi Nickolas-

    I have been using code you provided to another user a couple years ago (https://wordpress.org/support/topic/how-to-provide-post_title-post_content-post_excerpt/), and recently, the code stopped adding the caption to the uploaded image post. I have only one field with my upload form (Caption). In the plugin settings, I have specified to add images to the Media Library and to show Custom Fields in Media Library. I checked the WP database and the caption is stored in the postmeta table meta_key => _wp_attachment_metadata in a serialized array; however, the caption had been also added to the post table as post_title. This is no longer happening. The code I’m using is below.

    Jennifer


    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 );
    $caption = ( isset($userdata_fields[0]) ? $userdata_fields[0]["value"] : "" );
    $attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . wfu_basename( $file_path ),
    'post_mime_type' => $filetype['type'],
    'post_title' => $caption,
    '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";
    }

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

    (@nickboss)

    Hi, this is strange, I see that post_title property is set while creating the attachment.

    Is there any other plugin changing Media Items properties?

    Regards

    Nickolas

    Thread Starter justmigrating

    (@justmigrating)

    Nickolas –

    No other plugins are installed that I think could be interfering with the code you provided to store the Caption upon image upload. When I look in the WordPress postmeta table, I can see the Caption there in a serialized array in the _wp_attachment_metadata key, but I can’t figure out how to retrieve the value. The value of the key is below, Caption is at the end of the serialized array. I have tried the following code to retrieve the value of the Caption, but it does not return anything. Any advice?

    $my_data = wp_get_attachment_metadata( get_post_thumbnail_id( $postid ) ); $my_data = $my_data[‘WFU User Data’][‘Caption’];
    echo $my_data;

    a:6:{s:5:”width”;i:1200;s:6:”height”;i:1200;s:4:”file”;s:27:”2020/12/EnterpriseTheme.jpg”;s:5:”sizes”;a:3:{s:6:”medium”;a:4:{s:4:”file”;s:27:”EnterpriseTheme-400×400.jpg”;s:5:”width”;i:400;s:6:”height”;i:400;s:9:”mime-type”;s:10:”image/jpeg”;}s:9:”thumbnail”;a:4:{s:4:”file”;s:27:”EnterpriseTheme-150×150.jpg”;s:5:”width”;i:150;s:6:”height”;i:150;s:9:”mime-type”;s:10:”image/jpeg”;}s:12:”medium_large”;a:4:{s:4:”file”;s:27:”EnterpriseTheme-768×768.jpg”;s:5:”width”;i:768;s:6:”height”;i:768;s:9:”mime-type”;s:10:”image/jpeg”;}}s:10:”image_meta”;a:12:{s:8:”aperture”;s:1:”0″;s:6:”credit”;s:0:””;s:6:”camera”;s:0:””;s:7:”caption”;s:0:””;s:17:”created_timestamp”;s:1:”0″;s:9:”copyright”;s:0:””;s:12:”focal_length”;s:1:”0″;s:3:”iso”;s:1:”0″;s:13:”shutter_speed”;s:1:”0″;s:5:”title”;s:0:””;s:11:”orientation”;s:1:”0″;s:8:”keywords”;a:0:{}}s:13:”WFU User Data”;a:1:{s:7:”Caption”;s:26:”Enterprise WordPress Theme”;}}

    Thread Starter justmigrating

    (@justmigrating)

    Nickolas – turns out I still couldn’t get your code to work anymore, but I got what I needed with this code (in case anyone else sees this post).

    $the_query = new WP_Query( array(
    ‘author’ => $user_id,
    ‘post_status’ => ‘inherit’,
    ‘post_type’ => ‘attachment’
    )
    ));
    if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
    $the_query->the_post();
    $imagelink = get_the_guid();
    $postid = get_the_ID();
    $metadata = wp_get_attachment_metadata($postid);
    $caption = $metadata[‘WFU User Data’][‘Caption’];
    }
    }
    wp_reset_postdata();

    Plugin Author nickboss

    (@nickboss)

    ok thanks

    Regards

    Nickolas

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Store image caption’ is closed to new replies.