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
If you need to get the page ID you can easily do this by logging into your WordPress website admin and opening the page, post or custom post type for editing. The page id is visible in the address bar as shown in the screenshot below.
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 !
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With