I have a script that's called from my functions.php file (via an ajax .get) that needs to have access to WP's get_option() method in order to retrieve some values it needs to process.

However, although the file works great in the majority of sites where it resides, on just a few installations, I'm having problems with the script operating (chrome's javascript console reports a 404 (not found) on color.php.

//jQuery calls the file color.php

        ('<?php echo get_bloginfo('template_directory') ?>/color.php', 
        {theme: 'test', spot: '1'}, 
                doColor('#theme_header_color', data);

Here is the file color.php

echo 'all good';

In this case, I never get the echo since it appears that the require statement has failed to load the wp-blog-header.php

Is there an alternative way of including the header file in order to be able to call get_option()?

2 Answers 2


You shouldn't need to make direct calls to your theme files - use the AJAX API, and make all requests to admin-ajax.php (that way, WordPress'll be loaded for you, and you won't need to assume the file hierarchy in order to load it manually).

    '<?php echo admin_url( 'admin-ajax.php' ) ?>',

        "color_theme" : "test",
        "color_spot" : "1",
        "action" : "change_color"

    function( data ) {
        doColor( '#theme_header_color', data );

And in functions.php;

function __do_color_ajax()
    $theme = $_GET['color_theme'];
    $spot = $_GET['color_spot'];

    // do something

    die( 'AJAX output' );
add_action( 'wp_ajax_nopriv_change_color', '__do_color_ajax' );

See how the suffix of the hook, wp_ajax_nopriv_change_color, matches the action variable in the AJAX request? See this SE answer for more info.

Note I've also prefixed the other AJAX request variables, as you should namespace custom $_GET and $_POST variables in the same way you do PHP variables and functions.

Also check out the codex on the AJAX API.

  • Thanks for that TDM, however, I've got some code inside of color.php that I want to execute and pass the result back to ajax. Can you tell me how to include my color.php file in your example?
    – Scott B
    Jun 1, 2011 at 18:03
  • Instead of including the file, you would make a callback function and attach it to the appropriate action hook, as mentioned in the Codex article. Jun 1, 2011 at 18:24
  • BTW, if you really want to to it more like your original approach, I can tell you how, but I don't recommend it. Jun 1, 2011 at 18:25
  • Thanks, but since I'm using jQuery, I'm having a tough go of translating the pure Ajax example to my script. My first thought, using what I already have, was to pass the ABSPATH constant over to the color.php, but I'm having pathing issues with the slashes.
    – Scott B
    Jun 1, 2011 at 18:42
  • Just trying to determine how to pass the PHP callback function name in the jQuery. Would it be the value of the "action" argument? action = 'myPHPfunction'; ?
    – Scott B
    Jun 1, 2011 at 19:12

Just change the way you make the include in your color.php to:

  • This still makes assumptions about the filesystem. Why reinvent the wheel, when WordPress has already given you a Porsche 911 Turbo? ;) Jun 3, 2011 at 15:34

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.