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:
<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
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 usingadd_query_arg()
) you need to add them to the public query variables available toWP_Query
. These are built up whenWP_Query
instantiates, but fortunately are passed through a filterquery_vars
before they are actually used to populate the$query_vars
property ofWP_Query
.For your case :
and on your template page call the get methode like that :
More at the Codex : http://codex.wordpress.org/Function_Reference/get_query_var
I hope it helps !
Not sure if this will show anything but try turning on error reporting with:
at the top of your page before any other code.
Edit:
From the OP comments:
I usually discover errors like these only when they begin to defy everything I know about a language or an environment.
Try this:
If you see
… that means you’re not setting the size GET parameter. What URL are you using to access said page?
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:
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.
You may want to take a look at WP’s documentation about the WP Query object.
http://codex.wordpress.org/Query_Overview
It seems like wordpress uses the get_query_vars to method of the wp_query object to get queries.
You may also take a look at this one:
http://codex.wordpress.org/Custom_Queries
Although it is quite old post it might be of good use to let know…. There is one plugin that can ease handling of $_GET, $_POST and global variable declaration issues pretty well. http://wordpress.org/extend/plugins/wordpress-registry/
May be this can somebody
Please check your htaccess file. and have a rewrite rule there
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.