Shortcode with PHP issue “Undefined index”
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!
Leave an answer