How can I change the placeholder image for products in WooCommerce

Marc Wag­ner

Febru­ary 2, 2023

1 min read|

When Woo­Com­mer­ce is instal­led, a pla­ce­hol­der image for the pro­ducts is auto­ma­ti­cal­ly crea­ted and ente­red into the data­ba­se. In this tuto­ri­al I would like to show you how you can easi­ly replace this image.

option_{$option} #

First, upload the image you want to your Word­Press envi­ron­ment via the media libra­ry. Then you save the path to the file. You can find the path in the attach­ment details.

After that, go to the functions.php of your child the­me and add the fol­lo­wing PHP snip­pet the­re:

/**
 * @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 ins­tead of WooCommerce’s default pla­ce­hol­der image.

Have fun with it.

88e86fcb816eff22bc917094df2862d8dd5c0e978b333e6dd5f36f808990c261 96

Arti­kel von:

Marc Wag­ner

Hi Marc here. I’m the foun­der of Forge12 Inter­ac­ti­ve and have been pas­sio­na­te about buil­ding web­sites, online stores, appli­ca­ti­ons and SaaS solu­ti­ons for busi­nesses for over 20 years. Befo­re foun­ding the com­pa­ny, I alre­a­dy work­ed in publicly lis­ted com­pa­nies and acqui­red all kinds of know­ledge. Now I want to pass this know­ledge on to my cus­to­mers.

Hast du eine Fra­ge? Hin­ter­lass bit­te einen Kom­men­tar