0

I'm making a theme where I'm adding support for the custom background feature with this standar function:

// fig. 1

<?php

function custom_theme_support() {
        add_theme_support('custom-background', array(
                'default-color'    => '333333',
                'wp_head_callback' => '_custom_background_cb'
        ));
}
add_action('after_setup_theme', 'custom_theme_support');

?>

However I was usatisfied with the way the default function _custom_background_cb handles the settings, because it only gives the option to center the background image horizontaly. So i made this function in my functions.php file:

// fig. 2

<?php

function themeslug_custom_background() {

        $background = set_url_scheme(get_background_image());
        $color = get_background_color();

        if ($color === get_theme_support('custom-background', 'default-color')) { 
                $color = false;
        }

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

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

        if ($background) { 
                $style = "background: #$color url('$background') no-repeat fixed; background-size: cover; background-position: center;";
        }
?>
        <style type="text/css" id="custom-background-css">
                body.custom-background { 
                        <?php echo trim($style); ?>
                }
        </style>
<?
        }
?>

And changed the wp_head_callback seen in fig. 1 to:

// fig. 3

function custom_theme_support() {
        add_theme_support('custom-background', array(
                'default-color'    => '333333',
                'wp_head_callback' => 'themeslug_custom_background'
        ));
}
add_action('after_setup_theme', 'custom_theme_support');

However, the _custom_background_cb function is still called, instead of the themeslug_custom_background I created.

I am aware this function will void some of the background settings in the wp-admin/customize window, but this is for a very minemalistic theme, and its more important for me to center the background, than it is to be able to chage this setting in the wp-admin.

  • @ialocin I'm new here, so I'm not that familliar with the guidelines for questions, but I did not find anythig that said i could not thank people in advance for any help they might provide, I also do not understand why you removed the functions and php tags from my post, as this is the main content, could you please explain this reasoning to me? :) – Isak May 18 '15 at 14:18
  • 1
    Your approach is pretty much the text book one, so in my mind it should work. Do you have debugging enabled and excluded the possibility that there is another problem with your code? – Nicolai May 18 '15 at 14:18
  • It is not a big problem, but it is generally considered unnecessary noise, so I removed it along with the two superfluous tags. – Nicolai May 18 '15 at 14:19
  • 1
    Your code works. There is something else interfering. – s_ha_dum May 18 '15 at 14:59
  • 1
    Yes, of course! How else would I know that the code works? – s_ha_dum May 18 '15 at 18:49
0

You have

wp_head_callback

as parameter at your arguments array, which is wrong.

It should be

wp-head-callback

which is the correct spelling.

Because of the wrong spelling the default value for wp-head-callback which is _custom_background_cb has been used, as the wrong spelling is unknown to Wordpress - of course.

  • When I read through the code for the old function I saw it, I was just about to post a edit x) – Isak May 19 '15 at 10:37
  • That is actually where it hit me too :) Please delete your answer, we don't want to promote deprecated functions - thanks! @Isak – Nicolai May 19 '15 at 10:38
  • My pleasure! @Isak – Nicolai May 19 '15 at 10:43

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.