• Resolved Mostafa Shahiri

    (@mostafadeveloper)


    Hi

    I am creating a rest api plugin for WordPress, but I have a serious problem. Response time for the requests is not good. I tried to use short loading of WordPress. I checked loading time for different steps of WordPress in wp-settings.php file and I found do_action(‘init’) step takes long time. I don’t know when (in which step) responses of the REST API classes are created, but is it possible to provide and send these responses before do_action(‘init’) step?

    Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    You cannot do API requests without firing the “init” action. If you can identify the time consuming init callbacks and know they are not necessary for API responses, you could remove them from the callback stack just before “init” fires. Use the action ‘after_setup_theme’ to remove callbacks added by the theme and plugins. Only do so if REST_REQUEST is defined as true.

    You can learn what callbacks were added by examining the global $wp_filter array.

    Thread Starter Mostafa Shahiri

    (@mostafadeveloper)

    @bcworkz Thanks for your reply. I tried to print $wp_filter array, but it is a huge array of objects. Is there any way to find and remove the callbacks of specific plugins or themes?

    Thread Starter Mostafa Shahiri

    (@mostafadeveloper)

    I want to turn off loading theme and its files and callback functions, because my plugin responses are JSON strings and I don’t need HTML output. I don’t know how can I do it?

    Thread Starter Mostafa Shahiri

    (@mostafadeveloper)

    @bcworkz I try following code to remove query hook from $wp_filter, but after do_action(‘init’), the query hook is still available in $wp_filter array.

    
    function turnoff_hooks()
    {
    remove_all_filters('query',10);
    }
    add_action('after_setup_theme','turnoff_hooks');
    
    Moderator bcworkz

    (@bcworkz)

    If you intent is to bypass the theme, removing specific hooks isn’t very effective. In any case, API requests cause the wp_using_theme() function to return false. A lot of theme related code is not executed for API requests as it is, however the theme’s functions.php is still parsed.

    Why are you removing “query” hooks? I thought the problem lay in “init” hooks? Removing query hooks would not prevent queries, only disallow certain preprocessing added by other modules. In any case, the only reason I can see for your code not working would be if the 10 argument is incorrect. The correct value corresponds to the array key used within the WP_Hook::callbacks property in which the target callback is listed. For example, “the_title” filter might have a $wp_filter listing like

    ["the_title"]=> object(WP_Hook){
      ["callbacks"]=> array {
        [5]=> array {"wptexturize"}
      }
    }

    The priority of the wptexturize callback here is 5. This is just an example and does not reflect actual $wp_filter values. Also note that “query” in some cases fires very early so removing it from “after_setup_theme” may not be effective.

    Thread Starter Mostafa Shahiri

    (@mostafadeveloper)

    @bcworkz I searched again and I found a solution for minimum loading of WordPress with setting SHORTINIT constant as true. I tried to load necessary file for my plugin:

    
    if(SHORTINIT)
    {
    require( ABSPATH . WPINC . '/formatting.php' );
    require( ABSPATH . WPINC . '/capabilities.php' );
    require( ABSPATH . WPINC . '/class-wp-roles.php' );
    require( ABSPATH . WPINC . '/class-wp-role.php' );
    require( ABSPATH . WPINC . '/class-wp-user.php' );
    require( ABSPATH . WPINC . '/date.php' );
    require( ABSPATH . WPINC . '/user.php' );
    require( ABSPATH . WPINC . '/embed.php' );
    require( ABSPATH . WPINC . '/class-wp-embed.php' );
    require( ABSPATH . WPINC . '/class-oembed.php' );
    require( ABSPATH . WPINC . '/class-wp-oembed-controller.php' );
    require( ABSPATH . WPINC . '/http.php' );
    require( ABSPATH . WPINC . '/class-http.php' );
    require( ABSPATH . WPINC . '/class-wp-http-streams.php' );
    require( ABSPATH . WPINC . '/class-wp-http-curl.php' );
    require( ABSPATH . WPINC . '/class-wp-http-proxy.php' );
    require( ABSPATH . WPINC . '/class-wp-http-cookie.php' );
    require( ABSPATH . WPINC . '/class-wp-http-encoding.php' );
    require( ABSPATH . WPINC . '/class-wp-http-response.php' );
    require( ABSPATH . WPINC . '/class-wp-http-requests-response.php' );
    require( ABSPATH . WPINC . '/class-wp-http-requests-hooks.php' );
    require( ABSPATH . WPINC . '/rest-api.php' );
    require( ABSPATH . WPINC . '/rest-api/class-wp-rest-server.php' );
    require( ABSPATH . WPINC . '/rest-api/class-wp-rest-response.php' );
    require( ABSPATH . WPINC . '/rest-api/class-wp-rest-request.php' );
    require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-controller.php' );
    require(PATH_TO_MY_PLUGIN);
    return false;
    }
    
    

    In my plugin, I register routes and callbacks functions with register_rest_route function and callback functions get rest_request objects as input and return rest_response objects as output, but when I send a request, it returns nothing.

    Moderator bcworkz

    (@bcworkz)

    The problem with SHORTINIT is resolving additional file dependencies. WP file dependencies are hopelessly intertwined. I couldn’t begin to tell you if anything is missing. I will say that unless you’ve extended WP_REST_Controller class in your plugin, you probably need to at least require an object specific controller like WP_REST_Posts_Controller or what ever is appropriate to fulfill your requests.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘init hook creates a long delay for REST responses’ is closed to new replies.