admin管理员组文章数量:1395752
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!
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!
Share Improve this question asked Feb 11, 2020 at 13:00 Mathieu PréaudMathieu Préaud 2035 silver badges18 bronze badges1 Answer
Reset to default 3It's because if the user enters large="true"
, then $atts['small']
isn't defined. The normal solution is to use shortcode_atts()
to set default values for the parameters, but you're not putting the result of shortcode_atts()
into $atts
, you're just just extracting it. This means that $small
will be defined, but $atts['small']
won't be.
The proper way to solve this is to put shortcode_atts()
into a variable:
$atts = shortcode_atts( array(
'large' => false,
'small' => false,
'url' => ''
), $atts );
On a side note, it having a single size=""
attribute, with small
or large
as possible values, instead of separate attributes for each size, would probably be easier to manage.
本文标签: functionsShortcode with PHP issue quotUndefined indexquot
版权声明:本文标题:functions - Shortcode with PHP issue "Undefined index" 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744759256a2623653.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论