• Resolved silvaring

    (@silvaring)


    Original post here – https://ww.wp.xz.cn/support/topic/checking-if-tablepress-cell-output-contains-text-string?replies=1

    Hi all (incl Tobias), any help in this will be greatly appreciated. I have some code in my wordpress template that I’d like to add an ‘if’ condition to. The code is currently as follows…

    // to get the table location
    <?php
    $geturl=”http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&#8221;;
    ?>

    // to output the specific table cell value

    <?php echo do_shortcode(‘[table id=1 filter=”‘.$geturl.'” hide_columns=”0,2-10″ /]’);?>

    This code works fine, however I would like to add a condition so that if the above cell output is a certain text string (‘film’ for example) then it goes onto fetch data based a certain set of shortcodes, e.g

    <?php echo do_shortcode(‘[table id=1 filter=”‘.$geturl.'” hide_columns=”0-2,4-10″ /]’);?>
    <?php echo do_shortcode(‘[table id=1 filter=”‘.$geturl.'” hide_columns=”0-3,5-10″ /]’);?>

    rather than a set of slightly different shortcodes, e.g

    <?php echo do_shortcode(‘[table id=1 filter=”‘.$geturl.'” hide_columns=”0-4,6-10″ /]’);?>
    <?php echo do_shortcode(‘[table id=1 filter=”‘.$geturl.'” hide_columns=”0-5,7-10″ /]’);?>

    Any good suggestions on how I could make this work?

    https://ww.wp.xz.cn/plugins/tablepress/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    thanks for your post, and sorry for the trouble.

    First, make sure to properly escape your $geturl variable, and use the proper syntax:

    $geturl = "http://$_SERVER['HTTP_HOST']$_SERVER['REQUEST_URI']";

    Instead of directly printing the Shortcode, you should use the Template Tag functions and then check their return value, like

    $table_html = tablepress_get_table( array(
      'id' => '1',
      'filter' => $geturl,
      'hide_columns' => '0,2-10',
    ) );

    Then, you can perform your checks on the table HTML

    if ( stripos( $table_html, 'film' ) ) {
      // 'film' was found
    } else {
      // 'film' was not found
    }

    To print a table, you can then either echo the HTML, or use tablepress_print_table( ... ).

    Regards,
    Tobias

    Thread Starter silvaring

    (@silvaring)

    Thanks Tobias, I managed to get it working with your help, and I’ve also got it working by directly printing the short code like so…

    do_shortcode(‘[table id=1 filter=”‘.$geturl.'” /]’);
    if ( stripos( $geturl, ‘film’ ) ) {
    ?> etc…

    I’m unsure though why you suggest against that method though, is it because it can corrupt the table data that way?

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    no problem, you are very welcome! 🙂 Good to hear that this helped!

    I was a bit confused about what you want to search, I thought that it is the table output.

    I don’t recommend using do_shortcode() here for two reasons: The first one is performance and the second one is security, because you are not properly escaping the URL. Instead of that Shortcode from your example, please use

    tablepress_get_table( array(
      'id' => '1',
      'filter' => $geturl,
    ) );

    It’s the same output, but faster and more secure, as it can not be abused to insert arbitrary content.

    Best wishes,
    Tobias

    P.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Checking if Tablepress cell output contains text string’ is closed to new replies.