• I need to prevent execution of a shortcode in old IE browsers. I am hoping to avoid installation of another plugin. I attempted to make the shortcode conditional by encapsulating as follows

    <!--[if !(lte IE 8)]><!-->
    [shortcode]
    <!--<![endif]-->

    but that did not work. Does anyone have any suggestions for how to make a shortcode conditional based on the browser version?

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    You cannot embed shortcodes directly in HTML like that. This *might* work, but I have my doubts

    <!--[if !(lte IE 8)]><!-->
     <?php echo do_shortcode( '[shortcode]' ); ?>
    <!--<![endif]-->

    I think the PHP will execute anyway.

    You probably should test for the user agent in PHP, something like this

    <?php
     $ua =  $_SERVER['HTTP_USER_AGENT'];
     if (
          ( strpos( $ua, 'MSIE 8.0') == 0 ) &&
          ( strpos( $ua, 'MSIE 7.0') == 0 )
         ) {
       echo do_shortcode( '[shortcode]' );
       }
    ?>

    This offers a somewhat more efficient way of testing the user agent string:

    https://stackoverflow.com/questions/5302302/php-if-internet-explorer-6-7-8-or-9

    jwstanley

    (@jwstanley)

    I realise this is 7 months old, but I found it looking for an answer to the same question. So here is a solution for anyone like me who finds this thread looking for an answer. It does use an extra plugin though.
    I’m not a WordPress programmer, but it seems to be the case that WordPress recognises the comment block and ignores everything inside it, so it does not process the shortcodes inside the comment block.

    It’s possible to hide the opening <!–[if lte IE 9]> from WordPress at the critical moment using the plugin:
    Get Post Content Shortcode
    and using nested posts.
    So, in my solution:

    Post-1, with ID 001 contains only the IE if statement
    <!--[if lte IE 9]>
    or whatever you want it to be. This Post is only created to be “included” in the post you want to publish, using the Get Post Content plugin. This plugin has the ability to just get the content part of the post, without its title or any other fields.

    The post you want to publish needs to contain a shortcode from the Get Post Content plugin, to get the content of Post-1, and then your shortcode you want to be affected by the if statement in Post-1, and then the closing comment to close the if block:

    [post-content id="001"]
    [your-shortcode]
    <![endif]-->
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Short codes conditional on browser version’ is closed to new replies.