admin管理员组文章数量:1414621
I've created a shortcode for PHP file plugin template, now the template has two HTML design one with icons displayed and another shows only the Name.
Is it possible to trigger a bool when do_shotecode for icons in functions.php and then in the template file I check for bool and say if do_shortcode has parameter icon set to true then echo content with icons?
Paths for files I'm working on
- functions.php: child-theme/
- plugin template: child-theme/folderName/plugin_template.php
Basic code e.g
<?php if(do_shortcode_bool_with_icons == true ) { ?>
<h2>Print Items with Icons </h2>
<ul>
<li>Coffee</li>
</ul>
<?php } ?>
<?php if(do_shortcode_bool_text_only == true ) { ?>
<h2>Print Names only without icons</h2>
<ol>
<li>Coffee</li>
</ol>
<?php } ?>
I've created a shortcode for PHP file plugin template, now the template has two HTML design one with icons displayed and another shows only the Name.
Is it possible to trigger a bool when do_shotecode for icons in functions.php and then in the template file I check for bool and say if do_shortcode has parameter icon set to true then echo content with icons?
Paths for files I'm working on
- functions.php: child-theme/
- plugin template: child-theme/folderName/plugin_template.php
Basic code e.g
<?php if(do_shortcode_bool_with_icons == true ) { ?>
<h2>Print Items with Icons </h2>
<ul>
<li>Coffee</li>
</ul>
<?php } ?>
<?php if(do_shortcode_bool_text_only == true ) { ?>
<h2>Print Names only without icons</h2>
<ol>
<li>Coffee</li>
</ol>
<?php } ?>
Share
Improve this question
edited Sep 15, 2019 at 21:06
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Sep 15, 2019 at 15:10
Ibrahim HajjajIbrahim Hajjaj
31 bronze badge
1 Answer
Reset to default 0The do_shortcode()
function takes two parameters, string $content, bool $ignore_html = false
. If you can grab the content string before it is passed to the function, then you could do strpos()
or some kind of regex to check, which parameters are present in the shortocode string. But otherwise I don't think there's a way to do the kind of conditional check you're asking outside of do_shortcode()
.
A much more simpler, and more explicit, way to do the same thing is to include the conditional check inside your shortcode callback. Then you can have a default value (e.g. true/false) for rendering the icons and match it with the parameters passed to your shortcode.
For example your shortcode is used like this,
[your_shortcode icons="true"]
Your callback would then look like this,
add_shortcode( 'your_shortcode', 'your_shortcode_callback' )
function your_shortcode_callback( $atts ) {
$default = array(
'icons' => false,
);
$atts = shortcode_atts( $default, $atts, 'your_shortcode' );
if ( $atts['icons'] ) {
// return markup with icons
} else {
// return markup without icons
}
}
本文标签: Shortcode PHP file for two conditions passed when do shortcode
版权声明:本文标题:Shortcode PHP file for two conditions passed when do shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745155408a2645141.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论