5 Examples to use SHORTINIT in WordPress

The SHORTINIT constant in WordPress is used to perform a minimal bootstrap of the WordPress environment. It’s often used in scenarios where you want to access some core functionality without loading the entire WordPress framework, which can significantly improve performance for certain types of requests. However, it also means that many features of WordPress will not be available.

Here’s a typical file structure and some examples of common uses of SHORTINIT in a WordPress plugin.

Typical File Structure

my-plugin/
├── my-plugin.php
├── includes/
│   ├── shortinit-loader.php
│   └── api.php
└── public/
    └── assets/
        ├── js/
        └── css/

Example 1: Minimal API Endpoint

includes/shortinit-loader.php

<?php
define('SHORTINIT', true);
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');

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

// Check if the requested path is set
$path = isset($_GET['path']) ? $_GET['path'] : '';

$response = ['status' => 'ok', 'path' => $path];

echo json_encode($response);
exit;

Example 2: Custom Cron Job

includes/cron-job.php

<?php
define('SHORTINIT', true);
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');

// Perform minimal setup
global $wpdb;

// Your custom cron job logic here
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_%'");

echo 'Cron job completed successfully';
exit;

Example 3: Lightweight Data Fetching

includes/data-fetch.php

<?php
define('SHORTINIT', true);
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');

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

global $wpdb;
$table_name = $wpdb->prefix . 'your_custom_table';
$results = $wpdb->get_results("SELECT * FROM $table_name WHERE some_condition = 1");

echo json_encode($results);
exit;

Example 4: Simplified Authentication

includes/authenticate.php

<?php
define('SHORTINIT', true);
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');

$username = $_POST['username'];
$password = $_POST['password'];

if (empty($username) || empty($password)) {
    echo 'Username and password required.';
    exit;
}

$user = wp_authenticate($username, $password);

if (is_wp_error($user)) {
    echo 'Invalid login credentials.';
} else {
    echo 'Login successful. Welcome, ' . $user->display_name;
}
exit;

Example 5: Processing Form Submissions

includes/form-handler.php

<?php
define('SHORTINIT', true);
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');

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

// Assume $_POST['email'] and $_POST['message'] are set
$email = sanitize_email($_POST['email']);
$message = sanitize_text_field($_POST['message']);

// Insert the data into a custom table or send an email
global $wpdb;
$table_name = $wpdb->prefix . 'contact_form';
$wpdb->insert($table_name, [
    'email' => $email,
    'message' => $message,
    'submitted_at' => current_time('mysql')
]);

$response = ['status' => 'success', 'message' => 'Form submitted successfully'];
echo json_encode($response);
exit;

Summary

Using SHORTINIT in WordPress plugins can significantly improve performance by loading only the necessary parts of the WordPress core. The above examples demonstrate common uses such as minimal API endpoints, custom cron jobs, lightweight data fetching, simplified authentication, and processing form submissions. These scripts typically reside in the includes directory of your plugin and are invoked directly via URL routing or AJAX requests.

Tags:

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.