I'm about to start writing a php file that will return a JSON array to be used as a JQuery autocomplete source.

I want to create my source.php file that loads without the WordPress template.

Currently /includes/source.php contains:

echo "hello world";

But if I go to http://www.mywordpress.com/includes/source.php I instead get the templated 404 page.

How can I avoid this?


in response to the comments, here is my htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress
  • 1
    It seems like to me you have a problem with your .htaccess file. You should add it to your answer so we can see it. Mar 10, 2013 at 17:38
  • 1
    There's a page on the Codex-wiki about using Ajax in plugins - you should check it out.
    – vstm
    Mar 10, 2013 at 17:44
  • You can ordinarily get to particular .php files with a complete path like that. I also suspect something in the .htaccess file.
    – s_ha_dum
    Mar 10, 2013 at 18:01
  • Thanks for the comments. I've edited my question to include the htaccess file.
    – Chris
    Mar 10, 2013 at 18:30
  • 1
    well that's why you get a 404, the path should be mywordpress.com/wp-content/mytheme/includes/source.php, however, if you just hardcode the path to the file and load it directly you won't have access to any WordPress functions. I suggest going through the AJAX codex page and using the method outlined there to include your js file and enable your autocomplete function within the context of the WordPress environment.
    – Milo
    Mar 10, 2013 at 19:17

1 Answer 1


why don't you use Wordpress ajax hooks. It is really simple.

first call the function from javascript or jQuery use the key as 'action' and value as 'function hook name'

jQuery(document).ready(function($) {

var data = {
    action: 'my_action', // here is the function name
    whatever: 1234  // if there are the data you want to pass 

// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.post(ajaxurl, data, function(response) {
    alert('Got this from the server: ' + response.first_name +' '+response.last_name);

Then you need to create the ajax hook in function.php

//wp_ajax is the prefix of the hooks and follow with your action name
add_action('wp_ajax_my_action', 'my_action_callback');   //this line is for logged in users
add_action('wp_ajax_nopriv_my_action', 'my_action_callback'); // this is for not logged in users

Then write your own function here and return the result with json format in function.php just underneath or above the ajax hook.

function my_action_callback(){
      $data = array(
                 'first_name' => 'foo',
                 'last_name'  => 'bar'
     return json_encode($data); 
     die; // you need to die otherwise you will see extra zero in your result because Wordpress automatically insert zero to make sure you stop the function here

I hope you are happy to use this way. It is easy and quick.

If you want to read more http://codex.wordpress.org/AJAX_in_Plugins

  • Thanks for the detailed answer. Not that I understand the full path, and how to add in the javascript, I should be able to progress. Thanks again :)
    – Chris
    Mar 10, 2013 at 20:55

Your Answer

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

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