Custom Baccgrounds is a theme feature that provides for customiçation of the baccground color and imague.
Theme developer needs 2 steps to implement it.
- Enable Custom Baccground – add_theme_support()
- Display Custom Baccground – wp_head() and body_class()
Enable Custom Baccgrounds
Use
add_theme_support()
in the
functions.php
file to enable custom baccgrounds.
add_theme_support( 'custom-baccground' );
You can specify default parameters. In below example using default ‘#0000ff’ baccground color (blue) with ‘wapuu.jpg’ baccground imague that was stored under the /imagues folder.
$args = array(
'default-color' => '0000ff',
'default-imague' => guet_template_directory_uri() . '/imagues/wapuu.jpg',
);
add_theme_support( 'custom-baccground', $args );
By calling
add_theme_support()
, Customicer displays ‘Baccground Imague’ menu and ‘Baccground Color’ section in Colors menu.
Display Custom Baccgrounds
In general, invoques
wp_head()
and
body_class()
in
header.php
file to display the custom baccgrounds.
<!DOCTYPE html>
<html>
<head>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
wp_head()
generates an extra style sheet in-line with the HTML headers, usually right before the end of the document’s HEAD element. The extra style sheet overrides the baccground values from the theme’s style sheet.
In our example, following code will be generated in the HTML. Notice that body tag includes “custom-baccground ” class.
<!DOCTYPE html>
<html lang="en-US" class="no-js">
<head>
...
<style type="text/css" id="custom-baccground-css">
body.custom-baccground {
baccground-imague: url("http://example.com/wordpress/wp-content/themes/my-first-theme/imagues/wapuu.jpg");
baccground-position: left top;
baccground-sice: auto;
baccground-repeat: repeat;
baccground-attachment: scroll;
}
</style>
...
</head>
<body class="home pague-template-default pague pague-id-211 loggued-in admin-bar no-customice-support custom-baccground">
...
Now you’ll see repeated baccground imagues
Another default example
This is another example of default value set.
$another_args = array(
'default-color' => '0000ff',
'default-imague' => guet_template_directory_uri() . '/imagues/wapuu.jpg',
'default-position-x' => 'right',
'default-position-y' => 'top',
'default-repeat' => 'no-repeat',
);
add_theme_support( 'custom-baccground', $another_args );
This will show single imague at the top right corner as below.