0

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

8
  • 8
    You're probably passing a string instead of an array. Commented Jul 23, 2014 at 2:10
  • 2
    possible duplicate of How do I correct this Illegal String Offset? Commented Jul 23, 2014 at 2:11
  • The code that's calling this function seems to be passing a string instead of an array. You'd need to look there. Commented Jul 23, 2014 at 2:11
  • @MikeW I'm not even seeing a call to shortcode_slide in this entire script. I see a call add_shortcode. Did I miss the function call or is the OP's script incomplete? Commented Jul 23, 2014 at 2:13
  • thanks guys. i saw a few similar threads but because of my limited php knowledge it didn't really help. sorry if it's a duplicate, should i delete my post? Commented Jul 23, 2014 at 2:15

2 Answers 2

4

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' ) { 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.