Shortcode PHP file for two conditions passed when do shortcode

I've created a shortcode for PHP file plugin template, now the template has two HTML design one with icons displaye

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
Add a comment  | 

1 Answer 1

Reset to default 0

The 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

  }

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745155408a4614090.html

相关推荐

  • Shortcode PHP file for two conditions passed when do shortcode

    I've created a shortcode for PHP file plugin template, now the template has two HTML design one with icons displaye

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信