How declare Ajax functions ussing SHORTINIT

Build the PHP script that will handle the ajax resquest and send the ajax request directly to that filet (not to wp-admin/admin-ajax.php). In that file, first define SHORTINIT, then load WordPress manually and finally handle the ajax request.

define('SHORTINIT',true);
//IMPORTANT: Change with the correct path to wp-load.php in your installation
require_once ('../../../../wp-load.php');
//Load any WordPress module you may need from the include folder
//For exmaple:
//require( ABSPATH . WPINC . '/meta.php' );
//require( ABSPATH . WPINC . '/post.php' );

muestraMensaje();

function muestraMensaje(){
    echo "hola que tal";
    die();
}

Suppose you have named that file ajax.php and that it is in located in the URL mysite.com/wp-content/plugins/a-plugin/ajax.php. The, the javascript should be something like:

$.ajax({
    url: "http://mysite.com/wp-content/plugins/a-plugin/ajax.php",
})
.done(function( data ) {
    alert(data);
});

In the next example I use these WordPress functions: upadate_post_meta, get_post_custom, wp_send_json_success and wp_send_json_error. This modules are needed:

  • load.php: always needed to load WordPress
  • formatting.php: it contains sanitizing functions used by upadate_post_meta
  • meta.php: it contains functions related with post meta data and custom fields
  • post.php and revision.php: these modules contains post related functions needed when updating post data.

So, this is the PHP script:

define('SHORTINIT', true);
//IMPORTANT: Change with the correct path to wp-load.php in your installation
require( '../../../wp-load.php' );
require( ABSPATH . WPINC . '/formatting.php' );
require( ABSPATH . WPINC . '/meta.php' );
require( ABSPATH . WPINC . '/post.php' );
require( ABSPATH . WPINC . '/revision.php' );

cyb_uptdate_hits();

function cyb_uptdate_hits(){

    if( isset($_GET['postID']) ) {

        $post_id = intval( $_GET['postID']);

        if( $post_id > 0 ) {

            $get_meta = get_post_custom($post_id);

            if( isset($get_meta['hits'][0]) ) {
                $prev = intval($get_meta['hits'][0]);
            } else {
                $prev = 0;
            }

            update_post_meta($post_id, 'hits', $prev + 1);
            $res = array('postID' => $post_id, 'hits' => $prev + 1);
            wp_send_json_success($res);

        } else {
            wp_send_json_error('No post to update.');
        }

    } else {
        wp_send_json_error('No post to update.');
    }

    die('You die!');

}

This is the javascript I’m using:

(function($){
    $(document).ready(function(){
        //Update post hits counter
        if( typeof cyb_hits_data.postID !== 'undefined' && cyb_hits_data.postID != "0") {
            var update_hits = function(post_id){
                $.getJSON(cyb_hits_data.ajax_url,{
                    postID : post_id
                });
            }
            update_hits(cyb_hits_data.postID);
        }

    });
})(jQuery);

And the enqueue javascript:

add_action( 'wp_enqueue_scripts', 'cyb_hits_enqueue_scripts' );
function cyb_hits_enqueue_scripts() {
    wp_register_script('cyb-hits', plugins_url( '/js/hits.js', __FILE__ ), array( 'jquery' ) );

    wp_enqueue_script('jquery');
    wp_enqueue_script('cyb-hits');

    $theID = 0;
    if(is_single()) {
        $theID = get_the_ID();
    }

    $scriptData = array(
                  'ajax_url' => plugins_url( '/ajax_hits.php', __FILE__ ),
                  'postID'  => $theID
                );
    wp_localize_script('cyb-hits','cyb_hits_data',$scriptData);



}

Leave a Comment