How to Code a Default Featured Image in WordPress Customizer

Default Featured Image in WordPress Customizer

Adding a default featured image in WordPress is a useful feature to ensure that your posts always have a visual appeal, even if the author forgets to set a featured image. This can be achieved by using the WordPress Customizer and a bit of custom code. Here’s a step-by-step guide on how to do it.

Step 1: Set Up the WordPress Customizer

First, we need to add a section in the WordPress Customizer where the default featured image can be set. This can be done in the functions.php file of your theme.

function mytheme_customize_register( $wp_customize ) {
    // Add a section for the default featured image
    $wp_customize->add_section( 'mytheme_default_featured_image_section' , array(
        'title'      => __( 'Default Featured Image', 'mytheme' ),
        'priority'   => 30,
    ) );

    // Add a setting for the default featured image
    $wp_customize->add_setting( 'mytheme_default_featured_image' );

    // Add a control to upload the default featured image
    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'mytheme_default_featured_image_control', array(
        'label'      => __( 'Upload a default featured image', 'mytheme' ),
        'section'    => 'mytheme_default_featured_image_section',
        'settings'   => 'mytheme_default_featured_image',
    ) ) );
}
add_action( 'customize_register', 'mytheme_customize_register' );

In this code, we create a new section in the Customizer called “Default Featured Image” where users can upload an image to be used as the default featured image.

Step 2: Display the Default Featured Image

Next, we need to modify the template files to use the default featured image when no featured image is set for a post. This can be achieved by modifying the loop in your theme files (typically found in single.php, content.php, or index.php).

function mytheme_get_featured_image() {
    if ( has_post_thumbnail() ) {
        the_post_thumbnail();
    } else {
        // Get the default featured image from the Customizer setting
        $default_image = get_theme_mod( 'mytheme_default_featured_image' );
        if ( $default_image ) {
            echo '<img src="' . esc_url( $default_image ) . '" alt="' . get_the_title() . '">';
        }
    }
}

Use this function in place of the standard the_post_thumbnail() function in your template files.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <header class="entry-header">
            <?php mytheme_get_featured_image(); ?>
            <h1 class="entry-title"><?php the_title(); ?></h1>
        </header>
        <div class="entry-content">
            <?php the_content(); ?>
        </div>
    </article>
<?php endwhile; endif; ?>

In this code, we use our custom function mytheme_get_featured_image() to display either the post’s featured image or the default image set in the Customizer.

Step 3: Handle Different Sizes and Styling

You might want to ensure the default featured image respects the same size and styling as your regular featured images. This can be handled by adding appropriate classes or using the WordPress add_image_size function.

function mytheme_setup() {
    add_theme_support( 'post-thumbnails' );
    add_image_size( 'custom-size', 800, 600, true ); // Example size
}
add_action( 'after_setup_theme', 'mytheme_setup' );

function mytheme_get_featured_image() {
    if ( has_post_thumbnail() ) {
        the_post_thumbnail( 'custom-size' );
    } else {
        $default_image = get_theme_mod( 'mytheme_default_featured_image' );
        if ( $default_image ) {
            echo '<img src="' . esc_url( $default_image ) . '" alt="' . get_the_title() . '" class="wp-post-image custom-size">';
        }
    }
}

Conclusion

By following these steps, you can ensure that your WordPress site always has a featured image displayed for each post. This improves the visual consistency of your site and enhances the user experience. With the WordPress Customizer, you can easily allow theme users to set a default image without directly modifying the theme files.

By adding these snippets to your functions.php and template files, you can implement a robust default featured image feature in your WordPress theme.