Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to change themes background color using a plugin. For when I use this code,

function change_background_of_theme(){
?>
<style>

#main{background: yellow;}

</style>

<?php
}
add_action('init','change_background_of_theme');

This code only changes the bg color of the theme that use #main id. This basically leads to another question, is there a common id that themes use for background or something close to that. I appreciate your assistance. :)

-Ron.

share|improve this question
    
There is really no way to do that, almost every second theme uses different div structure. What you could do is let the user specify div or class. –  OriginalEXE Sep 6 '12 at 12:50
    
Thanks thats what I thought. :) –  Ronny Sep 6 '12 at 12:50
add comment

2 Answers 2

If the theme uses the custom-background functionality of WordPress, then WordPress outputs CSS code in the head which essentially says body.custom-background { ... } and it puts the chosen colors and image in there.

So most themes sorta expect that to be on the body itself, and to have a custom-background class on the body tag.

share|improve this answer
    
Thanks Otto... I see your point. :) –  Ronny Sep 6 '12 at 12:54
add comment

If the plugin uses the custom-background, then you can switch easily, updating the "theme_mods_{$name}" option:

/** Plugin Name: (#64373) Custom background Color */
function wpse64373_set_custom_background( $new_colour )
{
    $old_colour = get_theme_mod(
         'background_color'
        ,get_theme_support( 'custom-background', 'default-color' )
    );

    // Nothing to do here
    if ( $old_colour == $new_colour )
        return;

    return set_theme_mod( 'background_color', $new_colour );
}
function wpse64373_update_custom_background()
{
    // Not sure if we have to prepend a `#`
    wpse64373_set_custom_background( '009EE0' );
}
add_action( 'init', 'wpse64373_update_custom_background' );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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