jeudi 26 mars 2015

WP_load, shortinit for wordpress Ajax


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.





Aucun commentaire:

Enregistrer un commentaire