WooCommerce & Shop OperationsFeb 02, 2023
How to Replace the Placeholder Image for Products in WooCommerce
When you install WooCommerce, a placeholder image for products is automatically created and entered into the database. Today, I would like to demonstrate how you can easily replace this image in this guide.
option_{$option}
First, upload the desired image to your WordPress environment via the media library. Then, secure the path to the file. You can find the path in the attachment details.
Next, switch to the functions.php of your child theme and insert the following PHP snippet:
/**
* @param $value
* @param $option
*
* @return mixed|string
*/
function wc_change_default_image($value, $option): string
{
if ('woocommerce_placeholder_image' !== $option) {
return $value;
}
// Add the path to the image, e.g.:
$src = '/wp-content/uploads/2023/02/example.jpg';
// If the image does not exist return the default value
if (!file_exists(get_home_path() . $src)) {
return $value;
}
// return the image
return $src;
}
/*
* Filter: apply_filters( "option_{$option}", mixed $value, string $option )
*/
add_filter('option_woocommerce_placeholder_image', 'wc_change_default_image', 10, 2);
That's it. Now your image will always be used instead of the default placeholder image from WooCommerce.
Have fun with it.