4
votes

I have a php file which holds a script within my wordpress theme folder which i am trying to view directly via a relative path in the browser ie: www.mywebsite.com/wp-content/themes/my-theme-name/file-in-question.php

however wordpress is giving a 404 error as soon as i try to view it directly.

I would create a template within wordpress but for the script to work i need to view it with the .php extension in the browser since its basically a custom search to another script outside wordpress which returns results like so:

www.mywebsite.com/wp-content/themes/my-theme-name/file-in-question.php?controller=Listings ecc....

All I need is to be able to view the php file without wordpress giving me a 404 error

I should also mention that this happens with any other php file within the theme. I can acess .html files and images (placed them on purpose its only on php file extensions.

Thanks in advance.

2
If you're getting a 404 it has nothing to do with Wordpress. Check the URL. - Mike
Hi Mike, the url is there the file is there and the path is correct i believe wordpress is blocking direct access to it as a security thing but don't know how to get around it. - Jason
wordpress doesn't allow direct access to PHP files in the theme's directory. Why can't you put the file in the root directory, or create a folder in the root and put the file there? - Matanya
Good point but the only issue is that i need the header, footer and custom fields to also load in this file to keep the same look as the site. - Jason
Wordpress definitely does not block direct access, unless the script trying to be used checks for this. I have seen this used multiple times successfully. Can you please post your permalink structure / .htaccess file? - Richard Denton

2 Answers

0
votes

For security, WordPress wont let you go to that file directly. Your best bet is to give it a template name, assign a page to it then navigate to that page.

<?php
    /* Template name: Your template */
    code
?>

That should run the code you need and you can restrict access to public if you need to in the page settings.

0
votes

I managed by placing that file outside wordpress then called the functions within the wordpress site. Not neat but works.