0

We have been using wp-load.php for ajax load. it's for showing a popup when you click "preview". As you may know it takes very long time (3~4 sec) for get even a few texts.

We have searched and found that "short init" is the solution to make it faster load.

But it seems to be little hard to get wp functions. when we use "wp-load", we used these functions.

    Wordpress functions
    =================================
    get_post_meta
    apply_filters
    $wpdb( wp_prefix ) ===> to find multi network prefix
    get_option
    wp_upload_dir
    get_current_blog_id
    _e, __ 
    get_userdata
    get_post
    get_the_post_thumbnail
    wp_get_attachment_image_src
    get_permalink
    wp_get_object_terms
    wp_remote_retrieve_body
    wp_remote_get

    WPML
    ====================================
    icl_get_languages

How can I call/declare these functions to use shortinit function. we really need to make ajax load fast and need to use wordpress functions.

This is our codes sample. is this a correct way to do?

    <?php
    ini_set('html_errors', 0);
    define('SHORTINIT', true);

    require_once $_SERVER['DOCUMENT_ROOT' ] . "/wp-load.php";
    require_once ABSPATH . WPINC . '/formatting.php';
    require_once ABSPATH . WPINC . '/meta.php';
    require_once ABSPATH . WPINC . '/post.php';
    require_once ABSPATH . WPINC . '/user.php';
    require_once ABSPATH . WPINC . '/capabilities.php';

    header( 'Content-Type: application/json; charset:utf-8' );

    $wpk_result             = Array( "state" => "fail" );
    if( $post_id = $_REQUEST["post_id"] )
    {
        $post                   = get_post( $post_id );
        $wpk_this_author        = new WP_User( $post->author );
        $wpk_this_thumb     = '';

        // Other Informations
        $wpk_result         = Array(
            'state'             => 'success'
            , 'post_id'         => $post->ID
            , 'post_title'      => $post->post_title
            //, 'permalink'     => get_permalink( $post->ID )
            , 'thumbnail'       => $wpk_this_thumb
            , 'phone'           => get_post_meta( $post->ID, 'jv_item_phone', true )
            , 'website'         => get_post_meta( $post->ID, 'jv_item_website', true )
            , 'email'           => get_post_meta( $post->ID, 'jv_item_email', true )
            , 'address'         => get_post_meta( $post->ID, 'jv_item_address', true )
            , 'author_name'     => $wpk_this_author->display_name
        );
    }
    die( json_encode( $wpk_result ) );

We need to keep adding file names to define the functions we need? for getting thumnail images, it needs so many function files we need to require.

We have also checked "theme checker" plugin. ini_set('html_errors', 0); is a bad practice.

1
  • 2
    the point of shortinit is to load fewer files, but if you need a lot of API functions and find yourself having to include a bunch of files manually, then shortinit is kind of pointless. AJAX requests really shouldn't be taking 3-4 seconds though, you're seriously underpowered or something else is awry. I see millisecond response times on my cheap shared server.
    – Milo
    Mar 27, 2015 at 7:09

1 Answer 1

3

You should be using admin-ajax.php for your AJAX requests and WordPress has pretty good wrappers and actions to help you do that.

In terms of performance, 3-4s is pretty harsh. WordPress will typically serve a non-cached request in about 300ms out of the box, so some other theme or plugin is adding to that time, probably by doing something silly and time-consuming during init.

Also, calls to admin-ajax.php can also be cached and served from cache instantly, especially since your only variance seems like a post id.

Finally, you should probably be checking the status and post type of $post, because given your current code, anybody could read the fields you're providing for any post in your database, including private posts, protected ones, drafts, schedule posts, WooCommerce items, debug data or logs from some plugin storing stuff as post types, etc.

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.