Shortcode with PHP issue “Undefined index”

Question

I’m creating a shortcode to display images in different sizes: large, small and regular. The user has the option to set large="true" or small="true" when writing the shortcode to output the right image code.

I have a PHP issue with the following message on the Front End when the shortcode is active and the option large and/or small aren’t set to true or false:

Notice: Undefined index: large in /wp-content/themes/mysite/functions.php on line 1449

Notice: Undefined index: large in /wp-content/themes/mysite/functions.php on line 1451

Line 1449 is if( $atts['large'] == true ) and line 1451 is if( $atts['small'] == true )

Here is the PHP code of the shortcode:

add_shortcode ("img", "img_output");
function img_output($atts, $content="null") {
    extract( shortcode_atts( array(
            'large' => false,
            'small' => false,
            'url' => ''
    ), $atts ));
    if( $atts['large'] == true ) {
        return '<img src="' . $url . '" class="img-large" width="100%" height="auto" />';
    } else if( $atts['small'] == true  ) {
        return '<img src="' . $url . '" class="img-small" width="100%" height="auto" />';
    } else {
        return '<img src="' . $url . '" class="img-regular" width="100%" height="auto" />';
    }
}

Any help would be much appreciated, thanks!

0
, , Mathieu Preaud 4 years 2020-02-11T08:39:02-05:00 0 Answers 87 views 0

Leave an answer

Browse
Browse