admin管理员组文章数量:1327073
I am using this code so that users can input [photo no="1"], [photo no="2"], [photo no="3"]
etc.. in their posts. However I heard that extract
is an unsafe function to use. How could I modify this code to remove extract
?
function photo_shortcode($atts){
extract(shortcode_atts(array(
'no' => 1,
), $atts));
$no = is_numeric($no) ? (int) $no : 1;
$images = get_field('fl_gallery');
if ( !empty( $images ) ) {
$image = $images[$no];
return ' $image['url'] ' ;
}
}
I am using this code so that users can input [photo no="1"], [photo no="2"], [photo no="3"]
etc.. in their posts. However I heard that extract
is an unsafe function to use. How could I modify this code to remove extract
?
function photo_shortcode($atts){
extract(shortcode_atts(array(
'no' => 1,
), $atts));
$no = is_numeric($no) ? (int) $no : 1;
$images = get_field('fl_gallery');
if ( !empty( $images ) ) {
$image = $images[$no];
return ' $image['url'] ' ;
}
}
Share
Improve this question
asked Aug 4, 2020 at 16:15
JoaMikaJoaMika
6986 gold badges27 silver badges58 bronze badges
2 Answers
Reset to default 1It's very simple: Just assign the value of shortcode_atts()
(which is an array) to a variable and use it to access the shortcode attributes in the same way you access the items in any other arrays:
$atts = shortcode_atts( array(
'no' => 1,
), $atts );
// Use $atts['no'] to get the value of the "no" attribute.
$no = is_numeric( $atts['no'] ) ? (int) $atts['no'] : 1;
extract
brings variables from an array into local variables. It looks like in your case the only variable it does that with is $no
, so you can replace extract with this code which will do the same thing:
Replace:
extract(shortcode_atts(array(
'no' => 1,
), $atts));
with:
$arr = shortcode_atts(array(
'no' => 1,
), $atts);
$no = $arr['no'];
本文标签: phpRemove extract from function
版权声明:本文标题:php - Remove extract from function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742211192a2433744.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论