Assuming you are overwriting your permalinks , you will not be able to do this even though the file is inside the theme folder . This is due to the fact that WordPress, when with permalinks enabled, treats its routes through RewriteRule
s in .htaccess
. Assuming you go into the admin panel and set your URLs to be for example based on the post name, the following .htaccess
is created (I removed examples from an instance of WP that I have running in http://localhost/wp_testes/)
:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp_testes/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp_testes/index.php [L]
</IfModule>
# END WordPress
This answer in the gringo OS gives an excellent explanation of how these rules work. Adding this to WP's Hierarchy of Templates , it is easy to conclude that all requests made within your WP instance (in my case, /wp_testes/
forward), will be routed according to these rules, preventing direct access. If your teste.php
file does not conform to the template rules, the house will fall.
Solutions
Do not use permalinks.
It's dirty, I know. Your URLs will look awful, but this will allow you to have direct access to your file. In my case, I disabled the permalinks, and I made a direct access in http://localhost/wp_testes/wp-content/themes/twentyfifteen/teste.php?teste=123
, where teste.php
contained this complicated instruction:
<?php echo $_GET['teste']; ?>
and the result, as expected, was
123
Put your root file in WordPress
Placing the teste.php
file in the folder where the WP files are located (in my case, within the /wp_testes
folder), the permalink rules should not affect your requests. In this case, I made the request http://localhost/wp_testes/teste.php?teste=123
(with permalinks enabled) and got the expected result.
I do not know if you are developing a necessarily a theme, and whether or not you can include files in the WP root. I believe you are limited to these two solutions. But the sum of the two (permalinks with the in of the theme), I think there is no way.