Support » Themes and Templates » Background image code

  • Hi,

    First of all, sorry if my thread is in the wrong category, it’s my first time here ^^ .

    I’m using a Javascript code to add a background image to my blog.

    <!-- In my <head>: -->
    <script src="" type="text/javascript"></script>
    <!-- before the </body> : -->
    <script type="text/javascript">
    var options = {
    	position : "fixed"
    bgMax.init("", options);

    As you can see, I have to type manually the URL of my image. I would like to use the PHP variable (or something else if it’s not a variable) used by WordPress to write this HTML code:

    <style type="text/css" id="custom-background-css">
    body.custom-background { background-image: url(''); background-repeat: no-repeat; background-position: top left; background-attachment: fixed; }

    So I would like to know how WordPress marks the URL of the background image.

    I only have found that it is makes trought the wp_head() function but when I look for this function in generate_template.php, I don’t find what I’m looking for (the HTML code). I searching for few minutes now and still don’t find it.

    Does anyone know where this html code is in the WordPress files (or what is the PHP code that is about it)?

    Thank you in advance!


    P.S: In case if you need to know, my wordpress project is located there: .

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Andrew Nevins


    You could remove the and replace with a forward slash.

    bgMax.init("/wp-content/uploads/2012/09/london_street-wallpaper-2560x1440_bw.jpg", options);

    I’ll do it! Thank you for the advice!

    But it doesn’t respond completely to my need. I meant, I need to declare the file’s path in another way than manually. The reason is that if I want to publish my theme for other people, I don’t want them to type manually the path of the file (I want them to customize their background via the Administration Panel and not via the source code of the template).

    If the background image is registered in the SQL database, I was wondering if there was another possible method instead of making an SQL request to the database to get the image’s path.

    After a research, I finally found the code:

    function get_background_image() {
    	return get_theme_mod('background_image', get_theme_support( 'custom-background', 'default-image' ) );
    // few lines under
    function _custom_background_cb() {
    	// $background is the saved custom image, or the default image.
    	$background = get_background_image();
    	// $color is the saved custom color.
    	// A default has to be specified in style.css. It will not be printed here.
    	$color = get_theme_mod( 'background_color' );
    	if ( ! $background && ! $color )
    	$style = $color ? "background-color: #$color;" : '';
    	if ( $background ) {
    		$image = " background-image: url('$background');";
    		$repeat = get_theme_mod( 'background_repeat', 'repeat' );
    		if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
    			$repeat = 'repeat';
    		$repeat = " background-repeat: $repeat;";
    		$position = get_theme_mod( 'background_position_x', 'left' );
    		if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
    			$position = 'left';
    		$position = " background-position: top $position;";
    		$attachment = get_theme_mod( 'background_attachment', 'scroll' );
    		if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
    			$attachment = 'scroll';
    		$attachment = " background-attachment: $attachment;";
    		$style .= $image . $repeat . $position . $attachment;
    <style type="text/css" id="custom-background-css">
    body.custom-background { <?php echo trim( $style ); ?> }

    I don’t really know how to do what I want now… echo $background and echo get_background_image don’t work also…

    If still required, this works:

    Add this to functions.php:
    add_theme_support( 'custom-background' );

    And once you have chose your image, this should bring up the direct URL.
    <?php echo (get_background_image()); ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Background image code’ is closed to new replies.