$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>ajax - Load minimum WordPress environment|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

ajax - Load minimum WordPress environment

matteradmin11PV0评论

I made my own upload service for my website that is separate from WP, but uses WP to provide low level db functions and user verification. To do that, I include wp-load.php in my main script (Uploadify) but doing that seems to fill the server's memory with unnecessary components. Thanks to that, I'm seeing an error in my script that seems to point to a installed WP plugin.

How can I load only the core WP components and ignore plugins? Is this even possible?

I made my own upload service for my website that is separate from WP, but uses WP to provide low level db functions and user verification. To do that, I include wp-load.php in my main script (Uploadify) but doing that seems to fill the server's memory with unnecessary components. Thanks to that, I'm seeing an error in my script that seems to point to a installed WP plugin.

How can I load only the core WP components and ignore plugins? Is this even possible?

Share Improve this question asked Apr 3, 2012 at 18:09 MechEngineerMechEngineer 3732 silver badges11 bronze badges 1
  • 1 Check out BackPress. – soulseekah Commented Apr 3, 2012 at 18:28
Add a comment  | 

4 Answers 4

Reset to default 10

Disabling plugins entirely means you loose many advantages.

There are distributions of wordpress that go further and rip out posts and links etc, but they'll always lag behind WordPress core and tend not to survive as long.

Here are some things that could be done

Short Init

Putting this in your wp-config.php:

define( 'SHORTINIT', TRUE );

Or defining it somewhere before you load in wordpress, should reduce the loading process and pull it back to minimal core functions.

Secondary lightweight installs

Setup a second wordpress installation, with only the plugins and themes you want (if any). Then configure the wp-config.php to use the same wp-content directory and database values.

Backpress

WordPress, the original BBpress and glotpress amongst others are built around the BackPress library. You could use this instead of WordPress to do your work though numerous APIs and features may be missing or in need of re-implementation/porting

Option tables

I would also bear in mind that WordPress loads the entire options table into memory to cut down on queries, if you're saving any large values in there it'll impact on performance.

Exiting early

You could also try hooking into earlier functions in the WordPress load process and exiting the PHP process before WordPress is finished, but I can't advise on how safe that would be.

Selectively loading plugins

There is also this article on selectively loading plugins, but it does require hacks to the wordpress core files

For making is_user_logged_in() and current_user_can() work, I found similar answer here (Cant comment) The comparsion matches answers here, as:

Using define('SHORTINIT', true) + require('wp-load.php') + manually includes:

Pageload: 1.05 sek - included files: 43 files

Comparing: Using ONLY require('wp-load.php'):

Pageload: 1.35 sek - included files: 419 files

The time difference (0.3 sek) might differ from installs and PHP engines, but while validating many requests on one pageload -things adds up! /@Anna Ericson

It was a good idea to use a relative call to WP installed dir. From a Wordpress custom plugin "ROOT" like:

$wordpress = '../../../wp-load.php';

Then inside my plugin index.php:

define('SHORTINIT', true);
include_once $wordpress;

require_once ( ABSPATH . WPINC . '/class-wp-user.php' );
require_once ( ABSPATH . WPINC . '/class-wp-roles.php' );
require_once ( ABSPATH . WPINC . '/class-wp-role.php' );
require_once ( ABSPATH . WPINC . '/class-wp-session-tokens.php' );
require_once ( ABSPATH . WPINC . '/class-wp-user-meta-session-tokens.php' );
require_once ( ABSPATH . WPINC . '/formatting.php' );
require_once ( ABSPATH . WPINC . '/capabilities.php' );
//require_once ( ABSPATH . WPINC . '/query.php' ); // - might be useful
require_once ( ABSPATH . WPINC . '/user.php' );
require_once ( ABSPATH . WPINC . '/meta.php' );

wp_cookie_constants();

require_once ( ABSPATH . WPINC . '/vars.php' );
require_once ( ABSPATH . WPINC . '/kses.php' );
require_once ( ABSPATH . WPINC . '/rest-api.php' );
require_once ( ABSPATH . WPINC . '/pluggable.php' );

After this, user validation is working for me. Thanks to @Anna Ericson final words from original code:

For other task, running on one or two requests, tracking down other needed files might not be worth 0.3 sek. Skip the SHORTINIT constant and manually clutter.

Use BackPress, but i have small developments, where i use a default install and smaller source via const.

see on the wp-settings.php

// Stop most of WordPress from being loaded if we just want the basics.
if ( SHORTINIT )
    return false;

Set the const SHORTINIT on true in the wp-config.php and see the fast run. define( 'SHORTINIT', TRUE );

These are the files you get:

// All are located in ABSPATH.WPINC
'/load.php'
'/default-constants.php'
'/version.php'
'/compat.php'
'/functions.php'
'/class-wp.php'
'/class-wp-error.php'
'/plugin.php'
'/default-filters.php'
'/pomo/mo.php'

// Only Multisite
'/ms-blogs.php'
'/ms-settings.php'

Have you tried define('SHORTINIT', true);? I haven't tested it myself, but it seems to load very few aspects of WP core while retaining enough functionality, especially if all you want is to deal with the database.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far