0

Thought I'd take this nifty shortinit trick for a spin. However, when I include this in my custom ajax handler, I get no response from my callback. If I comment it, it works just fine. Anything changed?

<?php
//mimic the actual admin-ajax
define('DOING_AJAX', true);

if (!isset( $_POST['action']))
    die('-1');

ini_set('html_errors', 0);
define('SHORTINIT', true);
require_once('wp/wp-load.php'); 

//Typical headers
header('Content-Type: text/html');
send_nosniff_header();

//Disable caching
header('Cache-Control: no-cache');
header('Pragma: no-cache');


$action = esc_attr(trim($_POST['action']));
$allowed_actions = array(
    'posts',
    'status',
);

if(in_array($action, $allowed_actions)){
    if(is_user_logged_in())
        do_action('handle_tumblr_'.$action);
    else
        do_action('handle_tumblr_nopriv_'.$action);
}
else{
    die('-1');
} 


function tumblr_status() {
  echo 'whos there?';
  exit;
}

add_action('handle_tumblr_status','tumblr_status');
add_action('handle_tumblr_nopriv_status','tumblr_status');


function tumblr_posts() {
  echo 'k';
  exit;
}

add_action('handle_tumblr_posts','tumblr_posts');
add_action('handle_tumblr_nopriv_posts','tumblr_posts');
1
  • is it the is_user_logged_in that's causing problem? I tried putting a die('hello'); before that check but it still came back blank.
    – INT
    Commented Oct 4, 2014 at 15:47

1 Answer 1

1

You are using esc_attr(), which isn't available if you use define( 'SHORTINIT', true ); , see source. So you either have to load the formatting.php manually or have to ditch the define( 'SHORTINIT', true ); - actually like the example in the thread you are referring to shows.


As said in the comment, I checked out mentally after the first function that can't work, but you are right about do_action - can't tell you exactly though what you need for that. Anyway, you should probably just not use SHORTINIT and won't have problems. Just to note as much, personally I just do my AJAX tasks the Wordpress way.

6
  • Doh. Yeah, loaded up formatting.php and it seems to get me further. Seems do_actions is holding me up. I've included plugin.php and pluggable.php aswell, but it won't work, and from what I understand I shouldn't need more?
    – INT
    Commented Oct 4, 2014 at 16:39
  • 2
    Using SHORTINIT gives you access to the database code and some of the multisite stuff, and that's about it. None of the core WordPress helpers will be loaded. If you need those helpers in any way, just don't use SHORTINIT.
    – Otto
    Commented Oct 4, 2014 at 16:41
  • Yea, sorry, I kind of checked out mentally after the first function that can't work. @INT Either make sure you load all you need or, like Otto said, don't use SHORTINIT. Commented Oct 4, 2014 at 17:26
  • I totally get that, going with SHORTINIT and require what's needed will make the ajax run faster though. Gonna see if I can located what's needed.
    – INT
    Commented Oct 4, 2014 at 17:43
  • 1
    @INT mantain SHORTINIT is hard. You have to load all the thing you need, and all the things needed by the things that you need, and so on... Also, you need to check everithing works on every WordPress (major) update, because sometimes functions are moved from file to file (e.g. when deprecated, but not only in that case) and you can get fatal errors when before update everithing was fine. For same reason code may not work with older versions. Be careful with it and, just a tip, avoid to use it in plugin/themes to be shared or sold. Also, be sure to test your code with WP_DEBUG turned on.
    – gmazzap
    Commented Oct 4, 2014 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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