wordpressroots-sage

Sage Roots $_GET and $_POST variables


What is the best practice for getting $_POST and $_GET variables to template files? Because I can ofcourse use $_POST and $_GET in each template file. But I might be overlooking something. I cant imagine this is the way to do it.

If it is the best way please comment. If you have a better way of doing this, I am happy to hear you!

Cheers,

Martijn


Solution

  • Alright, I am here to answer my own question

    Step 1: Register your query variable function and hook it up :wink:

    function add_query_vars( $qvars ) {
    $qvars[] = 'var_x';
    $qvars[] = 'var_y';
    $qvars[] = 'var_z';
    return $qvars;
    }
    add_filter( 'query_vars', NAMESPACE . '\add_query_vars' );
    

    Step 2: refresh permalinks

    Step 3: Retrieve your variables

    <?php $var_x = get_query_var('var_x ', -1 ); ?>
    <?php $var_y = get_query_var('var_y ', -1 ); ?>
    <?php $var_z = get_query_var('var_z ', -1 ); ?>
    

    Let me know if this is the right way.