如何从产品图像EXIF元标题设置产品名称(帖子标题)

I try to add my products with as little human input as possible. Therefor I'm looking for a solution to grab the title tag which is in my added product image and put it in the Product name field on or before saving the product. Any attempts to achieve this are failing because WordPress "thinks" that no title is given (so no slug could be generated). At least I think that this is the case.

See screenshot of the field

This is the field I'm talking about

I tried to use a code snippet I found here on SO and to rework it to a working solution but I fail to get it right.

Here is the code I came up with:

function fcsp_set_title_on_save( $post_id ) {

$post_thumbnail_id = get_post_thumbnail_id( $post_id );
$filemeta = wp_get_attachment_metadata( $post_thumbnail_id, FALSE );

// Set this variable to false initially.
static $updated = false;

// If title has already been set once, bail.
if ( $updated ) {
    return;
}

// Since we're updating this post's title, set this
// variable to true to ensure it doesn't happen again.
$updated = true;
$title          = $filemeta['image_meta']['title'];

// Update the post's title.
wp_update_post( [
    'ID'         => $post_id,
    'post_title' => $title,
] );
}
add_action( 'save_post', 'fcsp_set_title_on_save' );

Any idea how to accomplish this?

Please put this code in function.php if this is woocommerce you are using (I think rather than custom post type )

if(class_exists('WC_Admin_Meta_Boxes')) {
class wcsave extends WC_Admin_Meta_Boxes {
public function __construct() {
    add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 );
    add_action( 'woocommerce_process_product_meta', 'WC_Meta_Box_Product_Data::save', 10, 2 );
}
public function save_meta_boxes( $post_id, $post ) {
    //$_POST enter your post data here, this will help to control the post request from product woocommerce
    //if product is updating don't execute image title code section
    if(!empty($post->post_title)) {
        return;
    }
    //if new product is being added.
    if(!empty($_POST['post_ID']) && $post_id == $_POST['post_ID']) {
        $post_thumbnail_id = get_post_thumbnail_id( $post_id );
        $attachment_data = get_post( $post_thumbnail_id,OBJECT ); 
        $title = count($attachment_data) > 0 ? $attachment_data->post_title : "PRODUCT-".$post_id;
        remove_action( 'save_post', array( $this, 'save_meta_boxes' ) , 1, 2 );
        wp_update_post( [
            'ID'         => $post_id,
            'post_title' => $title,
            'post_status' => $post->post_status
        ] );
        // re-hook this function
        add_action( 'save_post', array( $this, 'save_meta_boxes' ) , 1, 2 );
    }
}
}
new wcsave();
}

But please note, as I've tested you need at least some other info along with product image you are uploading like product description, rest it will save the image name as product title.

Thanks