add custom background wordpress functionality support to theme

In new wordpress version we can add the background image or color to wordpress websites. Many older wp themes has no support for custom background functionality.

add custom background wordpress

You can very easily add the custom background support to your wordpress theme. You just need to copy following code and put in your functions.php file.

add custom background wordpress

add custom background wordpress


add_filter( 'body_class', 'wpapi_body_class' );
function wpapi_body_class( $classes ) {
	$background_color = get_background_color();
	$background_image = get_background_image();

	if ( empty( $background_image ) ) {
		if ( empty( $background_color ) )
			$classes[] = 'custom-background-empty';
		elseif ( in_array( $background_color, array( 'fff', 'ffffff' ) ) )
			$classes[] = 'custom-background-white';
	}

	return $classes;
}

// Activate custom background and set callback function
if ( function_exists( 'add_theme_support' ) ) {
    $defaults = array(
    'default-color' => '000000',
    'default-image' => get_template_directory_uri() . '/img/background.png',
    'wp-head-callback' => 'my_theme_background_cb',
    'admin-head-callback'    => '',
    'admin-preview-callback' => ''
    );
    add_theme_support( 'custom-background', $defaults );
}

Add a Comment

Your email address will not be published. Required fields are marked *