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 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

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


Here is the file color.php

<?php
require_once('../../../wp-blog-header.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()?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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).

$.get(
    '<?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.

share|improve this answer
    
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 '11 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. –  Dougal Campbell Jun 1 '11 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. –  Dougal Campbell Jun 1 '11 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 '11 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 '11 at 19:12
show 1 more comment

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

require($_SERVER['DOCUMENT_ROOT'].'./wp-blog-header.php');
share|improve this answer
    
This still makes assumptions about the filesystem. Why reinvent the wheel, when WordPress has already given you a Porsche 911 Turbo? ;) –  TheDeadMedic Jun 3 '11 at 15:34
add 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.