WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using $_get inside a shortcode (4 posts)

  1. gregounours
    Member
    Posted 1 year ago #

    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

  2. TobiasBg
    Member
    Posted 1 year ago #

    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

  3. gregounours
    Member
    Posted 1 year ago #

    Awesome !!!

  4. TobiasBg
    Member
    Posted 1 year ago #

    Hi,

    no problem, you are very welcome! :-)

    Best wishes,
    Tobias

Topic Closed

This topic has been closed to new replies.

About this Topic