mantismamita
5/28/2015 - 1:23 PM

custom background

custom background

function _mytheme_custom_background_cb() {
    
    $is_big = (wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' )[0]) ? true : false;
    
    if( has_post_thumbnail() && $is_big ){
        $background= wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' )[0];
        $background_lg = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' )[0];
        $background_med = wp_get_attachment_image_src( get_post_thumbnail_id(), 'medium' )[0];
        $color = 'dddddd';
    } else {
        $background= get_background_image();
        $color = get_background_color();
    }
    if ( ! $background && ! $color )
        return;
    
    $style_lg = $color ? "background-color: #$color;" : '';
    $style_med = $color ? "background-color: #$color;" : '';
    $style_sm = $color ? "background-color: #$color;" : '';
    
    if ( $background ) {
        $image= " background-image: url('$background');";
        $image_lg = " background-image: url('$background_lg');";
        $image_med = " background-image: url('$background_med');";
    
        $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: center $position;";
    
        $size= " background-size: cover;";
    
        $attachment = get_theme_mod( 'background_attachment', 'scroll' );
        if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
            $attachment = 'scroll';
        $attachment = " background-attachment: $attachment;";
    
        $style_lg .= $image . $repeat . $position . $attachment . $size;

        $style_med .= $image_lg . $repeat . $position . $attachment . $size;
        $style_sm .= $image_med . $repeat . $position . $attachment . $size;
    }
    ?>
    <style type="text/css">
        body.custom-background { <?php echo trim( $style_sm ); ?> }
        @media (min-width: 500px) {
            body.custom-background { <?php echo trim( $style_med ); ?> }
        }
        @media (min-width: 800px) {
            body.custom-background { <?php echo trim( $style_lg ); ?> }
        }
    </style>
    

<?php }

function mytheme_custom_background(){
add_theme_support( 'custom-background', apply_filters( 'mytheme_custom_background_args', array(
    'default-color'          => '#ffffff',
    'default-image'          => '',
    'default-repeat'         => '',
    'default-position-x'     => '',
    'default-attachment'     => '',
    'wp-head-callback'       => '_mytheme_custom_background_cb',
    'admin-head-callback'    => '',
    'admin-preview-callback' => ''
) ) );
}
add_action( 'after_setup_theme', 'mytheme_custom_background' );