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:

define('SHORTINIT',true);
require_once ('../../../../wp-load.php');
require_once ('../../../../wp-config.php');

function muestraMensaje_callback(){
    echo "hola que tal";
    die();
}
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
 */
add_action('init','registraAjax');
function registraAjax(){
    wp_register_script('miScript', get_bloginfo('stylesheet_directory').'/js/ajax.js');
    //$miUrl=array('url'=>admin_url('admin-ajax.php'));
    $miUrl=array('url'=>get_bloginfo('stylesheet_directory').'/myAjax/ajax.php');
    wp_localize_script(miScript, 'MyAjax', $miUrl);
    wp_enqueue_script('miScript',get_bloginfo('stylessheet_directory').'/js/ajax.js');
}

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

Edit:

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

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

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.

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



}


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

Leave a Comment