How to pass a GET request to a file that does not belong to wordpress

Asked

Viewed 407 times

2

wordpress does not recognize files that do not belong to it, even though it is there in the theme folder, which is my case. How can a make an external file receive this information?

For example: I’m in a .page and there is a button with a parâmetro via GET, through a link. The page is in:

www.meusite.com.br/tema/page.php

The target file, which will receive the parameter is:

www.meusite.com.br/tema/src/arquivo.php

That one arquivo.php has only been placed there and there is no link with wordpress, but has a capture function via $_GET.

DETAIL : I don’t want to access the file through the front, ie, present it in Browse, but only make it perform its conditions.

  • 1

    Are archives .php that are in the theme folder, and you want to do something like arq.php?param=1? That’s it?

  • Do you want this . php to use the functions provided by WP? Or does this . php have code that does not depend on WP?

  • Calo Felipe, are files .php yes! And the parameter is type that you made! P. R. Ribeiro that . php depends on WP only for global $wpdb.

  • Guys, the problem is just that! Can’t get a GET Arq.php? param=1 DESSE Arq.php (which is part of WP) to another arq2.php, which is inside the theme, but is not "run" on the front!

  • when vc runs this file directly at the url, it works or rolls a 404? (I’m kicking the 404)

  • 1

    Works for wordpress pages, posts, etc... But for that external file not.

  • I THINK this is because of permalinks. How you try to access the file directly, via http://dominio.com.br/arq.php?param=1, he interprets this path as a page, which in its structure does not exist. And direct access to it, via wp_content/themes/tema/arq.php is blocked. I think the solution is to host it or outside the theme, or create it as a wordpress page template even...

  • 3

    @Lollipop, if you still have interest in this question, can you edit it to add details? . . . . Comments are not the way to improve a question. Thanks!

  • 1

    @brasofilo, was edited and offered a reward to those who give the best answer.

  • What I don’t understand is the button that stays on page.php and has to run something unrelated to WP on arquivo.php... gives the impression that it can be solved with an AJAX request, but without knowing the relationship, it is difficult to say if this is it...

Show 5 more comments

1 answer

1


Assuming that you are over-writing your permalinks, you won’t be able to do that even as the file is inside the theme folder. This is because of the fact of Wordpress, when with permalinks enabled, treat your routes through RewriteRules in the .htaccess. Assuming you go to the admin panel and set your Urls to be, for example, based on the post name, the following .htaccess is created (I removed the examples of a WP instance I have running on 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

That answer, in the SO gringo, gives an excellent explanation of how these rules work. Adding this to the Hierarchy of Templates from WP, it is easy to conclude that all requests made within your WP instance (in my case, from /wp_testes/ forward), will be routed according to these rules, preventing direct access. If your file teste.php not according to the template rules, the house will fall.

Solutions

  1. Do not use permalinks.

    It’s dirty, I know. Your Urls will look terrible, but it will allow you direct access to your file. In my case, I disabled the permalinks, and made a direct access to http://localhost/wp_testes/wp-content/themes/twentyfifteen/teste.php?teste=123, where teste.php contained this very complicated instruction:

    <?php echo $_GET['teste']; ?>
    

    and the result, as expected, was

    123

  2. Put your file on root in Wordpress

    Filing teste.php in the folder where WP files are located (in my case, inside the folder /wp_testes), 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 don’t know if you are necessarily developing 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 file within theme), I think there is no way.

  • 1

    This guess is what kills your answer. I need something concrete. Overall, it’s good, but incomplete in this sense. + 1

  • Well, I could say that I’m sure that there is no way, hahaha. I think it was more language building than anything else, but it’s worth the tip for the future!

  • If no one gives a better answer than yours, which by the way is great, I will take as a solution, because the explanation of .htaccess it was good.

  • Do not forget to give a +1 pro gringo who published the answer that Linkei there in the gringo SO. That there is to shine the eyes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.