0

I have a wordpress site and I want to create a normal php file. If I put it anywhere in my wp-content folder, I always get a page not found error when I try to access it. For example, I create a file: json.php, and I put it in the wp-content folder and try to acess it like that: www.example.com/json.php, but I get the error.

If I put it outside my wp-content folder, it works, but then I can not use the Wordpress functions like The Loop. How can I create a php file in wordpress and acess it normally?

1
  • Have you read this?
    – fuxia
    Commented Apr 10, 2013 at 17:16

1 Answer 1

4

For example, I create a file: json.php, and I put it in the wp-content folder and try to acess it like that: www.example.com/json.php, but I get the error.

That is because the path is wrong. If you put the file in your theme the path to the file is http://example.com/wp-content/themes/your-theme-name/your-file.php, not http://example.com/your-file.php. That path points to the server document root, not to a file in your theme directory.

WordPress and the server conspire to lie about the location of files, at least when pretty permalinks are on. If they are off you will notice that most requests go through index.php and pages are loaded via various $_GET parameters but are not loaded directly.

It is not clear what you are trying to do but you can

  1. Use the correct path to the file
  2. Put the file in the document root
  3. Create a custom page template that will work with WordPress' rewriting mechanisms
  4. Rewrite the path yourself using .htaccess rules.
  5. Use the AJAX API if appropriate
6
  • Perfect! I used the correct path file and it works now. If my file is at example.com/wp-content/json.php, can I still use php functions like the loop?
    – user25893
    Commented Apr 10, 2013 at 16:49
  • No, if you load the file directly you can't use any WordPress functions. Use option #3 if you need to do those.
    – s_ha_dum
    Commented Apr 10, 2013 at 16:50
  • If I do a page template, will I be able to have a blank php page, or will it have the site's layout (header, sections, body, etc) ?
    – user25893
    Commented Apr 10, 2013 at 16:52
  • Because all I want is to create json objects with information from The Loop
    – user25893
    Commented Apr 10, 2013 at 16:54
  • Then option #5: Use the AJAX API.
    – s_ha_dum
    Commented Apr 10, 2013 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.