Support » Fixing WordPress » Using $_get inside a shortcode

  • Resolved gregounours

    (@gregounours)


    I am new to this so bear with me πŸ™‚

    I am trying to use a url variable inside a shortcode on a page.
    Let’s say my url is http://tralala.com/pouet/?youpi=meuh

    I am using tablepress and would like to have the output of the folowing shortcode displayed: [table id=x filter=”meuh” show_columns=”2,4,6″/]

    I have looked around a bit and this seemed like the way to go:
    <?php
    $myvar = $_GET[‘youpi’];
    echo do_shortcode(“[table id=x filter=’$myvar’ show_columns=’6′);
    ?>

    Unfortunately that code display the below text on the page:

    $myvar = $_GET[‘youpi’];
    do_shortcode(“[table id=7 filter=’$myvar’ show_columns=’4,5,6′);
    ?>

    It does not make sense to me at all why it would just display that.

    Thanks in advance for your enlightening answers

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,

    thanks for your question.

    The best approach for this would be to create a new Shortcode that gets the $_GET parameter and passes that on to the TablePress template tag:

    add_shortcode( 'table_filter', 'tablepress_filter_shortcode' );
    function tablepress_filter_shortcode( $attributes, $content ) {
    	if ( ! empty( $_GET['table_filter'] ) ) {
    		$filter_term = $_GET['table_filter'];
    		$filter_term = preg_replace( '#[^a-z0-9]#i', '', $filter_term ); // only allow characters a-z, A-Z, and 0-9 in filter term
    		$attributes['filter'] = $filter_term;
    	}
    	return tablepress_get_table( $attributes );
    }

    Just add that as a small Extension by installing and activating https://github.com/downloads/TobiasBg/TablePress-Extensions/tablepress-shortcode-filter-get-parameter.zip as a regular WordPress plugin.
    After that, you would use this Shortcode:

    [table_filter id=x show_columns="2,4,6"/]

    The filter term is added automatically from the “table_filter” argument in the URL, i.e. from $_GET['table_filter'].

    Regards,
    Tobias

    Thread Starter gregounours

    (@gregounours)

    Awesome !!!

    Hi,

    no problem, you are very welcome! πŸ™‚

    Best wishes,
    Tobias

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Using $_get inside a shortcode’ is closed to new replies.