0

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 } ?>

1 Answer 1

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

  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.