To be clear : in Wordpress there is an option to change the body backgroung-image. I want to modify this option to permit to change the background-image of a particular section (named #section1) and not the background-image of the body.

I know that this question has already been asked by Emir Dupovac, but I applied the advices given by TheDeadMedic and it just doesn't work...

Link to the similar question

In the file functions.php I made 3 changes :

  1. I commented the part of the default code relating to the display of the background-image

    function espace_harmonie_body_classes( $classes ) {
    // Adds a class of custom-background-image to sites with a custom background image.
    /*if ( get_background_image() ) {
    $classes[] = 'custom-background-image';
  2. I put this code in my <template_name>_setup() function :

    // Image in section  
    $defaults = array(
        'default-color'    => 'ff0000',
        'default-image'    => '%1$s/images/image-section.jpg',
        'default-repeat'         => '',
        'default-position-x'     => '',
        'default-attachment'     => '',
        'wp-head-callback' => 'wpse_189361_custom_background_cb',
        'admin-head-callback'    => '',
        'admin-preview-callback' => ''
    add_theme_support( 'custom-background', $default);
  3. And this one just after the <template_name>_setup() function :

    function wpse_189361_custom_background_cb() {
        _custom_background_cb(); // Default handler
        $style = ob_get_clean();
        $style = str_replace( 'body.custom-background', '#section1', $style );
        echo $style;

Despite these modifications, when I try to add a background image in the admin interface, the background is applied to the body and not to my section. Could someone explain me why? Thank you.

  • Can you post the resulting $style?
    – jgraup
    Nov 6, 2016 at 17:25
  • Why not use CSS's background-image property on the section in question?
    – Cedon
    Nov 6, 2016 at 17:42
  • What's the current output from the custom background callback? PS: Please link to the mentioned question and it's solution and use the code format instead of using screenshots for the code, thanks
    – birgire
    Nov 6, 2016 at 18:02
  • jgraup : How can I have the result of $style? Nov 6, 2016 at 19:04
  • 1
    birgire : How can I check the output? I update the question right now. Nov 6, 2016 at 19:07

1 Answer 1


Okay! Finally I have found the solution! (even if I don't understand why we have to do this that way).

In fact we have not to modify the function.php file but the customizer.php with the code I quoted above (in the question).

Now I really feel released from that issue, but anyone could explain me why is it that file that we have to modify? I think I haven't really understood the role of the differents files.

The inc subdirectory

The inc subdirectory content

enter image description here

  • It should be functions.php in the current theme directory, not function.php. I'm not sure where the customizer.php file is located that you've edited.
    – birgire
    Nov 6, 2016 at 20:54
  • Sorry this is a lack of precision, I first edited functions.php with a 'S', and this file is in my directory. customizer.php is in a subdirectory named inc. Nov 6, 2016 at 21:20
  • Probably, if you look inside functions.php you will find that customizer.php is included there, if the user is an admin. By placing this block of customizer related functions in a different file, developers prevent it being loaded every time, also for ordinary users.
    – cjbj
    Nov 7, 2016 at 8:43
  • Yes indeed! Thank you @cjbj for the specifications. Here are the related functions calling the files in the inc directory: /** * Custom template tags for this theme. */ require get_template_directory() . '/inc/template-tags.php'; /** * Customizer additions. */ require get_template_directory() . '/inc/customizer.php'; Nov 8, 2016 at 21:24

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.