-1

When upgrading my website to PHP 8.0, then the following message will appear on the page:

Warning: Trying to access array offset on value of type bool in......../vc_row.php on line 155

and in

............/vc_row.php on line 159

This relates to the following line of code (line 155):

$style .= 'background-image: url('. $bg_image_src\[0\]. '); ';

and line 159:

$fixed_bg = '\<div class="ios-fixed-bg"\>\<div style="background-image: url('. $bg_image_src\[0\]. '); background-position: '. $background_position .'; background-repeat: '. $background_repeat .'; "\>\</div\>\</div\>';

The code for the function is:

if ( $background_type == 'image_bg' && $background_image != '' && $background_effect != 'crossfade' ) {
    if ( $background_effect == 'animation' ) {
        if( !preg_match('/^\d+$/',$background_image) ) {
            $animate_style .= 'background-image: url('. $background_image . '); ';
            $animate_style .= 'background-position: '. $background_position .'; ';
            $animate_style .= 'background-repeat: '. $background_repeat .'; ';
        } else {
            $bg_image_src = wp_get_attachment_image_src($background_image, 'full');
            $animate_style .= 'background-image: url('. $bg_image_src[0]. '); ';
            $animate_style .= 'background-position: '. $background_position .'; ';
            $animate_style .= 'background-repeat: '. $background_repeat .'; ';
        }
    } else {
        if( !preg_match('/^\d+$/',$background_image) ) {
            $style .= 'background-image: url('. $background_image . '); ';
            $style .= 'background-position: '. $background_position .'; ';
            $style .= 'background-repeat: '. $background_repeat .'; ';
            if ( $background_attachment != '' ) {
                $fixed_bg = '<div class="ios-fixed-bg"><div style="background-image: url('. $background_image . '); background-position: '. $background_position .'; background-repeat: '. $background_repeat .'; "></div></div>';
            }
        } else {
            $bg_image_src = wp_get_attachment_image_src($background_image, 'full');
            $style .= 'background-image: url('. $bg_image_src[0]. '); ';
            $style .= 'background-position: '. $background_position .'; ';
            $style .= 'background-repeat: '. $background_repeat .'; ';
            if ( $background_attachment != '' ) {
                $fixed_bg = '<div class="ios-fixed-bg"><div style="background-image: url('. $bg_image_src[0]. '); background-position: '. $background_position .'; background-repeat: '. $background_repeat .'; "></div></div>';
            }
        }
    }
}

Can someone please help me with the code so I can use PHP 8.0?

When I go back to PHP 7.4 the warning disappears.

Thanks for helping!

So far I haven't dared to change the code myself.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • The error means $bg_image_src must be a bool (probably `false`). And https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/#return says that wp_get_attachment_image_src will return false when no image is available. So there is your reason, there is not much need to be puzzled about it – ADyson Aug 06 '23 at 23:09
  • Thank you very much! If I understand correctly, the correct code would look like this (without brackets and false instead of zero?): Line 155 $style .= 'background-image: url('. $bg_image_src=false. '); '; and Line 159 $fixed_bg = '\
    \
    \
    \
    ';
    – Cquadrat Aug 07 '23 at 11:46
  • That doesn't appear to make any sense, no. What do you actually want the code to do? – ADyson Aug 07 '23 at 14:37
  • My apologies, I'm not a software developer. I have a problem with a code in a theme. The theme author hasn't contacted me for months, the theme reports this error when I update PHP and now I was hoping to fix the error myself. I'm sorry if I've asked for help that I'm not entitled to. – Cquadrat Aug 07 '23 at 16:30
  • Well, this site is for software developers to ask useful programming questions (as per the [tour]) and assist each other with the code they are writing. It's not a general helpdesk. If you're not a developer and you want some code fixing, generally that's something you pay for. It's very difficult for us to assist you with this because you don't have a clear idea what the purpose of this section of code is, you just know there's an error that you want to go away. That doesn't give us a clear specification by which we might reasonably alter the code. – ADyson Aug 07 '23 at 17:00
  • Also, then there's the scenario. For a start, I don't think upgrading from PHP 7.4 to 8 is actually the root cause here - a basic stripped-down demo shows that 7.4 will also still show this message: https://3v4l.org/9XRlb#v7.4.33 . The problem has always been there, but in 8.0 it was converted to a Warning instead of a Notice (a higher level of problem notification). At a guess your PHP settings are configured not to show notices, but to show warnings. On a live site this should all be logged to a file anyway instead of shown on screen! – ADyson Aug 07 '23 at 17:03
  • But as I said before, the real root of the issue is that the function wp_get_attachment_image_src (which is a built-in Wordpress function) returned null, because it could not find any information matching the provided attachment ID (from the `$background_image` variable in your code). So I'd guess either something has got deleted over time, or the `$background_image` variable isn't being given the correct value before it gets used. – ADyson Aug 07 '23 at 17:04
  • The best I can suggest as a way out of it is to simply not set the CSS background-image property when this occurs, e.g. `if ($bg_image_src != null) { $style .= 'background-image: url('. $bg_image_src[0]. '); '; }` so that it doesn't add that style property if there's no image data. But I've no idea if that will cause an issue with the visual display in your theme. – ADyson Aug 07 '23 at 17:08
  • You can do something similar with the other line which was causing an issue: `$fixed_bg = '
    ';`
    – ADyson Aug 07 '23 at 17:08
  • Thank you kindly for the answers. I'll try that. Of course, If I knew someone (in the German speaking area) who would help me with the code, I would of course pay for it. Otherwise I'll have to stay with PHP 7.4, because I don't get any error messages. – Cquadrat Aug 08 '23 at 08:33
  • At minimum I suggest you change your error settings in php so that errors and warnings are logged to a file in your live site, instead of being shown on the screen, that should always be done regardless. In a copy of the site used for testing only, of course you would switch them back on – ADyson Aug 08 '23 at 08:39

0 Answers0