1

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

4

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



}
| improve this answer | |
  • THanks! @cybmeta. Now I've the error: call to a member function main() but all said is the wp-load.php buy I include in my folder – Marcos Dec 23 '14 at 11:24
  • I don't understand what you mean with "but all said is the wp-load.php buy I include in my folder". Can you try to explain better? The code I posted is working, if you have modified it, please tell me. Note hat, as said in the answer, when using SHORTINIT WordPress is loaded with the minimum that is required and you have to include the modules you need manually. If you try to use functions that are not loaded you will get errors. I can not know what modules you need; I've added a working example that I'm using on a live site. – cybmeta Dec 23 '14 at 11:55
  • Sorry @cybmeta : I sais this because I load the file wp-load.php in my ajax.php. I looking for the solution to my problem "Call to a member function main()...." but I always found the solution is load the wp-load.php file but I 've this in my ajax.php like this: include_once ('../../../../wp-load.php'); and always have the same error – Marcos Dec 23 '14 at 12:00
  • Check that the path to wp-load.php is correct. I can not know where your ajax.php is located if don't tell me, if you say to me the location of ajax.php, I can say to you the correct path to wp-load.php to be included in ajax.php. Also, I recommend to not use inclue_once to load wp-load.php, use require_once insteaad as I post in the answer; this way you will be sure that the script will stop executing if the loading fails. – cybmeta Dec 23 '14 at 12:01
  • I think is correct because if I change the path I've error like failed to open stream, ABSPATH is not declare... – Marcos Dec 23 '14 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.