0

I need to set several default states for a theme's background. The idea is that the background changes upon choosing a color scheme. Everything is working and changing EXCEPT the background color. The color only changes if saved from the admin screen. Am I missing something?

add_action( 'after_setup_theme', 'theme_custom_background');

function theme_custom_background(){
  $theme = genesis_get_option('bf_style_selection');

  switch ($theme) {
    case 'bf-blue':
      $bg_color = 'def1fc';
      $bg_image = 'header-bg-blue.gif';
      break;
    case 'bf-black':
      $bg_color = 'dadada';
      $bg_image = 'header-bg-black.gif';
      break;
  }

  $defaults = array(
    'default-image'          => get_stylesheet_directory_uri().'/images/'.$bg_image,
    'default-color'          => $bg_color,
    'default-repeat'         => 'repeat-x',
    'default-position-x'     => 'top',
    'wp-head-callback'       => '_custom_background_cb',
    'admin-head-callback'    => '',
    'admin-preview-callback' => ''
  );

  add_theme_support( 'custom-background', $defaults);
}
1

I have hunted down the problem. It is the _custom_background_cb function in wp-includes/theme.php file. on line 1272 the comment "A default has to be specified in style.css. It will not be printed here."

Line 1272 is where the color is being retrieved:

$color = get_theme_mod( 'background_color' );

If we change that to:

$color = get_theme_mod( 'background_color', get_theme_support( 'custom-background', 'default-color' ) );

We can get a default color without the need to have it saved in theme_mod. We can still override the color with a saved value.

So, I duplicated the callback into my own function and this is what is working:

add_action( 'after_setup_theme', 'theme_custom_background');

function theme_custom_background(){
  $theme = genesis_get_option('bf_style_selection');

  switch ($theme) {
    case 'bf-blue':
      $bg_color = 'def1fc';
      $bg_image = 'header-bg-blue.gif';
      break;
    case 'bf-black':
      $bg_color = 'dadada';
      $bg_image = 'header-bg-black.gif';
      break;
  }

  $defaults = array(
    'default-image'          => get_stylesheet_directory_uri().'/images/'.$bg_image,
    'default-color'          => $bg_color,
    'default-repeat'         => 'repeat-x',
    'default-position-x'     => 'top',
    'wp-head-callback'       => '_bf_custom_background_cb',
    'admin-head-callback'    => '',
    'admin-preview-callback' => ''
  );

  add_theme_support( 'custom-background', $defaults);
}

function _bf_custom_background_cb() {
  // $background is the saved custom image, or the default image.
  $background = set_url_scheme( get_background_image() );

  // $color is the saved custom color.
  // Added second parameter to get_theme_mod() to get_theme_support() as we need default colors to show with needing to save.
  $color = get_theme_mod( 'background_color', get_theme_support( 'custom-background', 'default-color' ) );

  if ( ! $background && ! $color )
    return;

  $style = $color ? "background-color: #$color;" : '';

  if ( $background ) {
    $image = " background-image: url('$background');";

    $repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-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', get_theme_support( 'custom-background', 'default-position-x' ) );
    if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
      $position = 'left';
    $position = " background-position: top $position;";

    $attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
    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 ); ?> }
</style>
<?php
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.