$_GET and WordPress

I want to add a custom php file to a WordPress to do a simple action.

So far I have in my theme index.php file:

Read More
<a href="myfile.php?size=md">link</a>

and the php is

<?php echo "hello world"; ?>

<?php echo $_GET["size"]; ?>

<?php echo "hello world"; ?>

The link, once clicked, displays:

hello world

Is WordPress taking over the $_GET function and I need to do some tricks to use it? What am I doing wrong?

Edit:

<?echo "hello world";?>
<? 
  if (array_key_exists('size', $_GET))
    echo $_GET['size'];
?>
<?echo "end";?>

Ouputs :

hello world

Related posts

Leave a Reply

7 comments

  1. See the solution :

    In order to be able to add and work with your own custom query vars that you append to URLs, (eg: www.site.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.

    For your case :

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

    and on your template page call the get methode like that :

    $size_var = (get_query_var('size')) ? get_query_var('size') : false;
    if($size_var){
       // etc...
    }
    

    More at the Codex : http://codex.wordpress.org/Function_Reference/get_query_var

    I hope it helps !

  2. Not sure if this will show anything but try turning on error reporting with:

    <?php
       error_reporting(E_ALL);
       ini_set('display_errors', true);
    ?>
    

    at the top of your page before any other code.

    Edit:

    From the OP comments:

    silly question, but are you sure you
    are viewing the results of your latest
    changes to the file and not a cached
    copy of the page or something? Change
    “hello world” to something else.
    (Sorry grasping at straws, but this
    happened to me before) – Zenshai

    ahaha, the person that
    were doing the changes didn’t changed
    the correct file. It’s working now –
    marcgg

    peer programming fail ^^ – marcgg

    That would be an “or something”,
    can’t tell you how many times i’ve
    done something like that. Glad you
    were able to figure it out in the end.
    – Zenshai

    I usually discover errors like these only when they begin to defy everything I know about a language or an environment.

  3. Try this:

    <?echo "hello world";?>
    <? 
      if (array_key_exists('size', $_GET))
        echo $_GET['size'];
    ?>
    <?echo "end";?>
    

    If you see

    hello worldend
    

    … that means you’re not setting the size GET parameter. What URL are you using to access said page?

  4. WordPress does not take $_GET over. Are you sure that you are passing the variable correctly?

    If you hardcode the variable in a url, make sure it is of this form:

    YOUR_SITE_PATH/?variable_name=variable_value
    

    please not the “/” at the end of the url, before the “?”

    I cannot see your index.php code, but make sure that the variable “size” is either set manually in the url or from a submitted form. If you use a form, make sure that you use the method=”GET”. If you use method=”POST”, then your variable will be in $_POST[‘size’].

    Hope this helps.

  5. May be this can somebody

    Please check your htaccess file. and have a rewrite rule there

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase <<relative_url>>/
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . <<relative_url>>/index.php [L]
    </IfModule>
    # END WordPress
    

    Please change << relative_url>> with the relative url as compared to your domain

    I faced the same issue and my wordpress was installed on a godaddy server . But after changing the .htaccess file the issue got resloved.