How to randomly change background color in WordPress?

800 439 Manisha Vasani

Method 1: Add Random Background Color in WordPress Using Code

This method requires you to add code into your WordPress files.
First you need to add this code to your theme’s functions.php file

******************************************************************
functionwpb_bg() {
$rand= array(‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’);
$color=’#’.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].
$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
echo $color;
}
******************************************************************

This function simply generates a random hex color value and echoes it.
Now you need to edit your theme’s header.php file. Locate the <body> tag line, it will look like this:

******************************************************************
<body <?php body_class(); ?>>
******************************************************************

Replace it with this line:

******************************************************************
<body <?php body_class(); ?> style=”background-color:<?php wpb_bg();?>”>>
******************************************************************

Save your changes and then visit your website to see the code in action.

Method 2: Add Random Color Stripes Using Fabulous Background Colors

This method is easier and is recommended for beginners who do not want to edit their WordPress theme files.

First, you need to install and activate the Fabulous Background Colors plugin.
The plugin works out of the box, and there are no settings for you to configure.

You can now visit your website, and you will see colorful stripes as background color on your website. These stripes will fade and change colors elegantly after every 5 seconds.

Leave a Reply

Your email address will not be published.