Creating an ajax call in wordpress. What do i have to include to access wordpress functions

I am performing an jquery ajax request inside my wordpress. This calls an internal php script.
This php script needs to be able to access certain wordpress features like… functions.php
which is simple for me to include.
What i cant do is access info like the current wordpress user, the $wpdb object.
My question is… is there some wordpress file which i can include which gives me access to all that data (and functions.php).
I hope you understand what i am accessing as i am aware that was probably THE crappest explaination in the world
😀

Related posts

Leave a Reply

4 comments

  1. THE BAD WAY (as pointed out by others)

    When I created some custom PHP to use with wordpress I included the wp-load.php file. Which then loads everything required, including $wpdb.

    require_once('wp-load.php'); // relative path from your PHP file
    
    global $wpdb;
    $wpdb->show_errors = TRUE; // useful for when you first start
    

    I found it was a decent starting point for a quick fix. However you have to remember this will load in a lot more functionality than you may actually require. Thus resulting in slower performance times.

    THE GOOD WAY

    Once functionality became more complex the ‘bad’ implementation wasn’t proving to be all that great. So I moved onto writing plugins instead. The WordPress codex contains good information on working with AJAX and plugins: http://codex.wordpress.org/AJAX_in_Plugins

    In the most basic form you will need to register your AJAX hook:

    // 'wp_ajax_foo' is the hook, 'foo' is the function that handles the request
    add_action( 'wp_ajax_foo', 'foo');
    

    You will also need the corresponding function (in this case foo):

    function foo() {
        // handle the AJAX request
        $bar = $_POST['bar'];
    }
    

    Then in your JavaScript you identify which hook to use with the action attribute but leave out the wp_ajax part:

    $.post(ajaxurl, { action: 'foo', bar: true }, function(response) {
        // do something with response
    });
    
  2. I typically set up an action hook in functions.php to listen for AJAX requests based on a prefix, like ‘wp_ajax_*’. You will need to have a reference to wp-load.php in your javascript as well, which can be added using wp_head. Once this is set up, just use a variable called “action” in your AJAX request to indicate which function you want to use.

    // add javascript reference to wp-load.php as ajaxurl
    function core_add_ajax_url(){
        ?>
        <script type="text/javascript">var ajaxurl = "<?php echo site_url( 'wp-load.php' ); ?>";</script>
        <?php 
    }
    add_action('wp_head', 'core_add_ajax_url', 1 );
    
    // process all wp_ajax_* calls
    function core_add_ajax_hook() {
        /* Theme only, we already have the wp_ajax_ hook firing in wp-admin */
        if ( !defined( 'WP_ADMIN' ) && isset($_REQUEST['action']) ){
            do_action( 'wp_ajax_' . $_REQUEST['action'] );
        }
    }
    add_action( 'init', 'core_add_ajax_hook' );
    
    // Hook your function to the 'wp_ajax_*' for processing
    function my_function(){
        // do some things and then return JSON
    }
    add_action( 'wp_ajax_my_function', 'my_function' );
    

    Your Javascript request would look similar to the following:

    jQuery.postJSON( 
        ajaxurl, // request url
        { action: 'my_function' }, // request parameters
        function (response){ // callback
            // handle the response
        }
    );
    
  3. You shall never include neither wp-load.php, nor wp-config.php – this is a bad practice, and this article explains why: http://ottodestruct.com/blog/2010/dont-include-wp-load-please/. Also, WordPress.org team does not recommend including any WordPress core files directly.

    Instead, you can utilise the functionality of admin-ajax.php – don’t be deceived by ‘admin’ part – it is used in both front-end and back-end scripts. Here is an example: http://wp.smashingmagazine.com/2011/10/18/how-to-use-ajax-in-wordpress/