Skip to content

Instantly share code, notes, and snippets.

@mantismamita
Last active May 1, 2023 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mantismamita/23e47b5b317d9d761aa0 to your computer and use it in GitHub Desktop.
Save mantismamita/23e47b5b317d9d761aa0 to your computer and use it in GitHub Desktop.
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' );
@antoshechka
Copy link

Hello! Your function is very useful for my blog. Can you help? I switched to php8.1. Sometimes this errors appear. I'm getting "Warning: Trying to access array offset on value of type bool in Warning: Trying to access array offset on value of type bool in ... functions.php on line xxx" at the line "$is_big = (wp_get_attachment_image_src( get_post_thumbnail_id (), 'large' )[0]) ? true : false;". How can I fix this? Thank you!

@mantismamita
Copy link
Author

Hi, I haven't done WordPress or php in a while but I think you can try

$thumbnail_id = get_post_thumbnail_id();
$is_big = false;

if ($thumbnail_id) {
    $image_src = wp_get_attachment_image_src($thumbnail_id, 'large');
    if (is_array($image_src) && isset($image_src[0])) {
        $is_big = true;
    }
}

@antoshechka
Copy link

Thank you very much! I found a temporary workaround: I installed a plugin with similar functionality and it works. But I will definitely come back to your work results! Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment