Carlo Del Fabbro
Carlo Del Fabbro

Reputation: 11

PHP Warning: Illegal string offset in Wordpress

I'm hoping someone can help me figure out what may be happening with some PHP errors. They read like this:

"Warning: Illegal string offset 'type' in /home/awh/public_html/wp-content/themes/Avada/shortcodes.php on line 440"

The 'guilty lines' are 440,445,448,453, I've marked the lines in the code below (hope that's ok?)

The website is www.advancewithhealth.com and the errors seem to be generated due to some errors in the slider PHP code (it's a family friend so I'm not sure what changes she may have accidentally). I've included the guilty lines below:

//////////////////////////////////////////////////////////////////
// Slide
//////////////////////////////////////////////////////////////////
add_shortcode('slide', 'shortcode_slide');
function shortcode_slide($atts, $content = null) {
    $str = '';
    if($atts['type'] == 'video') { // <-- 440
        $str .= '<li class="video">';
    } else {
        $str .= '<li class="image">';
    }
    if($atts['link']): // <-- 445
    $str .= '<a href="'.$atts['link'].'">';
    endif;
    if($atts['type'] == 'video') { // <-- 448
        $str .= $content;
    } else {
        $str .= '<img src="'.$content.'" alt="" />';
    }
    if($atts['link']): // <-- 453
    $str .= '</a>';
    endif;
    $str .= '</li>';

    return $str;
}

Thanks in advance for any help!

Warmly, Carlo

Upvotes: 0

Views: 4684

Answers (2)

Beuvema
Beuvema

Reputation: 1

Today I ran into the same issue and I solved it by changing the code calling the slider similar to:

[slider][slide link=""]image link[/slide][slide link=""]image link here[/slide][/slider]

In my case the link="" was missing.

Upvotes: 0

doublesharp
doublesharp

Reputation: 27609

This is happening because you are not passing in the type attribute to your arguments for the shortcode. You can avoid the error by using:

[slide type='video'/]

That said, the correct way to account for this programmatically and reliably is to use shortcode_atts() function to set default values on your $atts array.

// the default $atts values
$defaults = array( 'type' => 'default value' );

// use array merge to override values in $defaults with those in $atts
$atts = shortcode_atts( $defaults, $atts );

// now the index will be set even if it wasn't passed into the shortcode
if( $atts['type'] == 'video' ) {

If you don't want to set any default values (even though you could default to false or something), you can also just check that the index is set on the array before accessing using isset(). I would recommend using the method above.

if( isset( $atts['type'] ) && $atts['type'] == 'video' ) { 

Upvotes: 4

Related Questions