• OBJECTIVE: load a separate header image for mobile agents for better performance
    STRATEGY:
    1. conditionally set the 'default-image' key of the $header_settings array that is passed to add_theme_support(). Logs show that it is set successfully.
    2. set the css background url by retrieving header_image().

    I have the following in my theme setup in functions.php:

    $ismobile = wp_is_mobile();
    $themeroot = get_teplate_directory_uri();
    $header_images = array(
      'mobile' => array(
        'url' => $themeroot . '/images/header-mobile.jpg',
        'thumbnail_url' => $themeroot . '/images/header-mobile-thumbnail.jpg',
        'description'   => __( 'one', 'test' )
        ), //array[0]
      'desktop' => array(
        'url' => $themeroot . '/images/header-desktop.jpg',
        'thumbnail_url' => $themeroot . '/images/header-desktop-thumbnail.jpg',
        'description'   => __( 'two', 'test' )
      ) //array[1]
    ); //$header_images
    
    register_default_headers($header_images);
    
    $header_settings = array(
      'default-image' => $themeroot . '/images/header',
      // 'uploads'    => true,
      'width'         => $ismobile? '299px' : '1696px',
      'height'        => $ismobile? '400px': '712px',
      'default-text-color' => '000',
      'flex-width'         => true,
      'flex-height'        => true,
    ); //header_settings
    
    header_settings['default-image'] .= $ismobile? '-mobile.jpg' : '-desktop.jpg';
    //log shows that default-image is set correctly
    add_theme_support('custom-header', $header_settings);

    Now in my header.php I have the following at the end of the head section after the call to wp_head():

    <?php if (has_header_image()) : ?>
          <style>
            header {
              background: url('<?php header_image(); ?>');
              max-width: 100%;
              height: auto;
            }
          </style>
        <?php endif; ?>

    has_header_image() returns false, and even if I skip the condition, header_image() returns null. It is possible to set the header image in the customizer, which loads the same image on desktop and mobile agents, but the default doesn’t work, although the value set for default-image is correct.

    Thanks

    • This topic was modified 7 years, 10 months ago by ehsanamini.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m not sure how you’re getting null, but the default-image path would be invalid. Your last $header_settings['default-image'] line in the first snippet that appends the rest of the filename is missing the initial $

Viewing 1 replies (of 1 total)
  • The topic ‘header_image() returns null despite registered default header image’ is closed to new replies.