Include WordPress (WP) Core without database

Remember short-link to MUST-HAVE WP plugins: puvox.software/wordpress 

Use WP core functionality without MySQL/DB

In very rare case, some people might want to use WP‘s nice functions in their projects, but without need of any Database. Unfortunately, WP is not meant to be used in that way, however there can be made some hacks.

Step 1

Create i.e. my_wp_loader.php and insert this code (just change wordpress path):

<?php
define( 'WP_PATH_', __DIR__.'/hidden_wordpress_path/' );

function include_wp_without_db()
{
    define( 'SHORTINIT', true );
    define( 'SHORTINIT_WITHOUT_DB', true );
    $wp_config_PATH = WP_PATH_ .'/wp-config.php';

    // check if needed modification is already done
    $wp_settings_PATH = WP_PATH_ .'/wp-settings.php';
    $addition = ' if (SHORTINIT_WITHOUT_DB) return false;';
    $target_hint = 'require_wp_db();';
    $content = file_get_contents($wp_settings_PATH);
    if (strpos($content, $target_hint.  $addition) === false )
        file_put_contents($wp_settings_PATH, str_replace($target_hint, $target_hint.$addition, $content));

    //check if wp-config exists
    if (!file_exists($wp_config_PATH)) 
        copy(WP_PATH_ .'/wp-config-sample.php', $wp_config_PATH);
    require_once( $wp_config_PATH );
}
include_wp_without_db();
?>

Step 2

Now, in any your project file, you can call basic WP functions like this:

<?php
require(__DIR__.'/my_wp_loader.php');

echo sanitize_key('asdg:?/2\'fdewrgoff3-rfj');

And you will have sanitized output!

Leave a Comment

Your email address will not be published. Required fields are marked *

WordPress › Error

There has been a critical error on this website.

Learn more about troubleshooting WordPress.