How do I directly access a PHP file located in my themes folder?

admin2025-06-02  2

I have a file in my theme's folder called "test.php".

If I go to www.mysite/wp-content/themes/my-theme/folder/test.php the theme loads which I assume is a 404 error handled by WP. If I change the file name to be test.html I can access it no problem.

I tried adding the following code to my functions.php file (to add a rewrite rule):

add_rewrite_rule( 'test.php$', $relative_directory . '/folder/test.php [L]', 'top' );

but even having the htaccess rule didn't seem to do anything.

Any idea what could be causing this?

I have a file in my theme's folder called "test.php".

If I go to www.mysite/wp-content/themes/my-theme/folder/test.php the theme loads which I assume is a 404 error handled by WP. If I change the file name to be test.html I can access it no problem.

I tried adding the following code to my functions.php file (to add a rewrite rule):

add_rewrite_rule( 'test.php$', $relative_directory . '/folder/test.php [L]', 'top' );

but even having the htaccess rule didn't seem to do anything.

Any idea what could be causing this?

Share Improve this question asked Jul 16, 2012 at 16:27 GazillionGazillion 3171 gold badge5 silver badges11 bronze badges 6
  • 2 You shouldn't be loading php files directly in the first place. What exactly does the file need to do? There's likely a better way to handle it. – EAMann Commented Jul 16, 2012 at 17:39
  • I have a PHP file that I use to go get some data from my database to serve via ajax but right now going to that file does nothing but server a wordpress 404. – Gazillion Commented Jul 16, 2012 at 17:52
  • 2 WordPress can get the data for you, and has built-in AJAX support. Using WP for data access gives you the added benefit of caching, sanitizing, escaping, and everything else you can use $wpdb for. – EAMann Commented Jul 16, 2012 at 19:09
  • How do you contain your ajax calls to your theme folder? I don't understand why it has to be taken out of it when everything else can be defined internally. – Gazillion Commented Jul 17, 2012 at 15:48
  • 3 You register your ajax calls in functions.php using a filter. Then you make the call to admin-ajax.php and WordPress passes it along. There's a plugin-based example in the Codex but it works the exact same way for themes. – EAMann Commented Jul 17, 2012 at 16:29
 |  Show 1 more comment

3 Answers 3

Reset to default 3

Assuming you use the correct URL to access the file, everything is being handled in your web server level and WP doesn't run at all, so WP based solutions like changing rewrite rules will not help you.

It is most likely that there is some web server configuration that sends the 404 for any access to a php file at that folder (guess you should try if other php files there also return 404). You might be able to fix the web server configuration to match your needs, but as @EAMann commented, it is not the right way to write ajax for WP.

Are you using RewriteRules in your .htaccess file? You should be able to access PHP files directly then. The first two lines with a RewriteCond make sure the rewrite to WordPress’ index.php does not happen if the file or directory exists.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /your-wp-site/index.php [L]

You have to create a custom WP template for that file to use display itself. Otherwise it is a stand alone file trying to be executed without permission or hooks. in this case you would need to add the file php code directly through WP and create a Page.

You can also open the "page.php" in your selected theme directory save it as "test.php" in that same directory and paste the entire content of your "test.php" PHP code inside the division of your new "test.php" file.

Something like this:

<main id="main" class="site-main" role="main">
<?php
//Your PHP Code Here!
?>
</main>

This just shows the basic Division. You will need. If you need the entire other site content to display along with the test.php you will need to create a content-test.php file under the "templates-parts" directory in your theme.

Again, same thing except your gonna open "content-page.php" this time and save it as "content-test.php" under the "templates-parts" directory and past the test.php code inside the where you would like this to show.

Back to the new "test.php" and replace the following line:

get_template_part( 'template-parts/content', 'page' );

with:

get_template_part( 'template-parts/content', 'test' );

The down side to this method is that when WP updates, it may or may not leave that file in tact vs. the first option which calls the php file as a post or page.

Hope that helps!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748862034a314337.html

最新回复(0)