Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Have you read this? –  toscho Apr 10 '13 at 17:16
add comment

1 Answer 1

up vote 5 down vote accepted

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
share|improve this answer
    
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? –  Maxwell Apr 10 '13 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 Apr 10 '13 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) ? –  Maxwell Apr 10 '13 at 16:52
    
Because all I want is to create json objects with information from The Loop –  Maxwell Apr 10 '13 at 16:54
    
Then option #5: Use the AJAX API. –  s_ha_dum Apr 10 '13 at 17:01
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.