How can I change the placeholder image for products in WooCommerce
Marc Wagner
February 2, 2023
When WooCommerce is installed, a placeholder image for the products is automatically created and entered into the database. In this tutorial I would like to show you how you can easily replace this image.
option_{$option} #
First, upload the image you want to your WordPress environment via the media library. Then you save the path to the file. You can find the path in the attachment details.
After that, go to the functions.php of your child theme and add the following PHP snippet there:
/** * @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/beispiel.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 about it. Now your image will always be used instead of WooCommerce’s default placeholder image.
Have fun with it.
Artikel von:
Marc Wagner
Hi Marc here. I’m the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.