How declare Ajax functions ussing SHORTINIT

The question:

I try to use SHORTINIT in my wordpress to do Ajax more fast, but I try to declare my functions and don’t return any value.

My code is:

require_once ('../../../../wp-load.php');
require_once ('../../../../wp-config.php');

function muestraMensaje_callback(){
    echo "hola que tal";
add_action('wp_ajax_muestraMensaje', 'muestraMensaje_callback');
add_action('wp_ajax_nopriv_muestraMensaje', 'muestraMensaje_callback');

And in my functions I’ve this code:

 *  Ajaxs function
function registraAjax(){
    wp_register_script('miScript', get_bloginfo('stylesheet_directory').'/js/ajax.js');
    wp_localize_script(miScript, 'MyAjax', $miUrl);

What can be the problem? If I called to my muestraMensaje I didn’t have any value.


Now I’ve a new problem: give me the error:

Fatal error: Call to a member function main() on a non-object…line

I looking for the solution but everybody said this error is for not include wp-load.php but I’ve include this file….

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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.

//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' );


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

Suppose you have named that file ajax.php and that it is in located in the URL The, the javascript should be something like:

    url: "",
.done(function( 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' );


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

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

        //Update post hits counter
        if( typeof cyb_hits_data.postID !== 'undefined' && cyb_hits_data.postID != "0") {
            var update_hits = function(post_id){
                    postID : post_id


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' ) );


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

    $scriptData = array(
                  'ajax_url' => plugins_url( '/ajax_hits.php', __FILE__ ),
                  'postID'  => $theID


All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment