Okay, so spending a couple of hours tinkering with how to require a featured image, and have the error come up on the Edit Product page rather than in the WP_Die screen and confues users, the idea came to me that I could create an additional, required wcv custom field that would be hidden with CSS. The value of that field – and its associated meta-key – would get updated automatically depending on whether the product has a featured image attached, in other words if has_post_thumbnail()
returns TRUE or FALSE. Since a FALSE result would update the value of the associated field, and the field is required, voila, a data-error message gets thrown.
I have it working… almost. I’ll mention what I need help with below.
Here’s the code in the product-edit form:
<div class="all-100">
<!-- Media uploader -->
<div class="wcv-product-media">
<?php WCVendors_Pro_Form_helper::product_media_uploader( $object_id );
// Hidden field - Pass the has_thumbnail() value to its metatag value & throw an error on the page if there's no custom field
// do_action( 'check_for_featured' , $object_id );
// Create the hidden field and the associated meta-key.
$has_thumb = get_post_meta( $object_id , 'wcv_custom_product_has_thumbnail', true );
$has_thumb == 1 ? $f = '1' : $f = '' ;
WCVendors_Pro_Form_Helper::input( array(
'type' => 'text',
'post_id' => $object_id,
'id' => 'wcv_custom_product_has_thumbnail',
'label' => __('Has Featured Image', 'wcvendors-pro'),
'placeholder' => __( 'Has Featured Image', 'wcvendors-pro' ),
'wrapper_start' => '<div class="hidden-field">',
'wrapper_end' => '</div>',
'value' => $f,
'custom_attributes' => array(
'data-rules' => 'required',
'data-error' => __( 'Please add a Featured Image.', 'wcvendors-pro') )
) );
do_action( 'check_for_featured' , $object_id );
?>
</div>
The Media Uploader in the code content above is the one that comes standard with the product-edit.php template, so the code I’ve added is below.
Here’s what I’ve got in the associated plugin file (it could run via the functions.php file just as well):
// REQUIRE THE FEATURED IMAGE
function check_product_featured_image( $product_id ) {
global $product;
global $post;
$post_type = get_post_type( $product_id );
$post_thumb = has_post_thumbnail( $product_id );
if ( $post_thumb && $post_type == 'product' ) {
$has_featured = "1";
}
else {
$has_featured = "0";
}
update_post_meta( $product_id , 'wcv_custom_product_has_thumbnail' , $has_featured );
}
add_action( 'check_for_featured' , 'check_product_featured_image' );
add_action( 'pre_post_update' , 'check_product_featured_image' );
add_action( 'wcv_save_product_meta', 'check_product_featured_image', 10 );
The field updates successfully, except in the one condition that matters once – when there’s no existing featured image on form load, then the user uploads one. Since the field doesn’t update until the form is saved, the unchanged meta data in my custom field doesn’t change, and still throws the error (I think this may have been the issue with the plugin suggested in this thread as well).
So… How can I get the metadata and field to update when the image is added or removed? It would be great if there’s a place to hook into, or a function to call, and I figure there must be, because the “Add/Remove” text changes depending on there being an image, updating in real time.
Thanks for your help!