Sunday, September 18, 2022
HomeWordPress Developmentphp - WooCommerce: write featured picture dimensions to customized fields in product'

php – WooCommerce: write featured picture dimensions to customized fields in product’


The important thing right here is to make use of the save_post hook and have it solely run the code when the put up kind is product (or no matter your product put up kind is).

Here is the code — I made the idea that you really want the total dimension of the featured picture:

add_action( 'save_post', 'save_product_featured_image_dimensions' );

operate save_product_featured_image_dimensions( $post_id ) {

    if ( get_post_type( $post_id ) != 'product' ){
        return;
    }

    $post_thumbnail_id = get_post_thumbnail_id( $post_id );
    $attachment = wp_get_attachment_image_src( $post_thumbnail_id, 'full' );

    if ( $attachment ) {
        $width = $attachment[1];
        $peak = $attachment[2];
    }

    update_post_meta( $post_id, 'product_featured_image_width', $width );
    update_post_meta( $post_id, 'product_featured_image_height', $peak );

}

Additionally, you most likely know this, however it appears value mentioning — you may after all additionally seize the picture dimensions of a product’s featured picture with out saving these to the metadata of the product, utilizing code just like the above. However I am assuming you wish to do one thing particular that will require the values to be within the put up meta.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments