WordPress and $_GET Params

This is an odd problem that I have never run into before with WordPress.

I have a site, permalinks enabled.

Read More

The url can be http://mysite.com/page-name/?anyParamName=testing

then when I use print_r($_GET); or echo $_GET["anyParamName"] I get an empty array or nothing respectively.

A pure PHP file works fine so its not a server issue. Does WordPress do anything with rewiring the get params? Really puzzled by this.

Related posts

Leave a Reply

4 comments

  1. For custom parameters you should register them with WordPress:

    add_action('init','wpse46108_register_param');
    function wpse46108_register_param() { 
        global $wp; 
        $wp->add_query_var('anyParamName'); 
    }
    

    Then the value of ‘anyParamName’ can be found with get_query_var

    $anyParamNameValue = get_query_var('anyParamName').
    
  2. know that this is old question, but we can freely access any post/get parameters inside ‘init’ hook, (before WordPress changes it) and that is documented here, look:

    add_action( 'init', 'process_post' );
    
    function process_post() {
         if( isset( $_POST['unique_hidden_field'] ) ) {
              // process $_POST data here, read and modify as you wish
         }
    }
    
  3. n order to be able to add and work with your own custom query vars that you append to URLs (eg: "http://example.com/some_page/?my_var=foo" – for example using add_query_arg()) you need to add them to the public query variables available to WP_Query. These are built up when WP_Query instantiates, but fortunately are passed through a filter ‘query_vars’ before they are actually used to populate the $query_vars property of WP_Query.

    So, to expose your new, custom query variable to WP_Query hook into the ‘query_vars’ filter, add your query variable to the $vars array that is passed by the filter, and remember to return the array as the output of your filter function. See below:

    function add_query_vars_filter( $vars ) {
      $vars[] = "my_var";
      return $vars;
    }
    add_filter( 'query_vars', 'add_query_vars_filter' );
    

    For further details please have a look
    https://codex.wordpress.org/Function_Reference/get_query_var

  4. I had the same problem:

    1. the page I accessed had permalink /focus.
    2. the main folder had a child folder with the same name (focus).

    The right page was loaded (not the folder), but the _GET parameters were empty.
    I renamed the folder (in the file system) and the _GET array is now getting as expected.