Create a custom and ligther WP ajax using SHORTINIT

WordPress provides admin-ajax.php for handling AJAX requests, but it can be too heavy for specific applications as it loads the entire core. If you need something lighter, you can use SHORTINIT. Below, we show you how to implement a custom file called custom-admin-ajax.php.

What is SHORTINIT?

SHORTINIT is a constant in WordPress that limits the functionalities loaded. When set to true, only the following elements are loaded:

  • The wp-load.php file.
  • Basic WordPress functions like wpdb, wp_safe_redirect, and sanitize_text_field.
  • The database connection and essential utilities for working with data.

This makes requests much faster by avoiding the full WordPress system, including plugins and themes.

Create the custom WP Ajax file

Create a file named custom-admin-ajax.phpin the wp-admin/ directory alongside admin-ajax.php.


 

Setup in the active theme

In the functions.php file of the active theme, we’ll register the JavaScript file, the AJAX event, and a shortcode to display the HTML button.

 

Register the script and AJAX event

 admin_url( 'custom-admin-ajax.php' ),
    ] );
}
add_action( 'wp_enqueue_scripts', 'enqueueCustomAjaxScript' );

 

Handle the action in PHP

Define the custom action to process the AJAX request and return the local and UTC time.

 $localTime,
        'utc_time'   => $utcTime,
    ] );
}
add_action( 'wp_custom_ajax_show_time', 'handleShowTime' );

 

Shortcode to display the button

Show Time
'; } add_shortcode( 'show_time_button', 'renderAjaxButton' );

 

Create the JavaScript file

Create a file named custom-ajax.js in your theme directory with the following content:

document.addEventListener('DOMContentLoaded', function() {
    const button = document.getElementById('show-time-btn');
    const output = document.getElementById('time-output');

    button.addEventListener('click', function() {
        fetch(customAjax.url + '?action=show_time')
            .then(function(response) {
                return response.json();
            })
            .then(function(data) {
                if (data.success) {
                    output.innerHTML =
                        '

Local Time: ' + data.data.local_time + '

' + '

UTC Time: ' + data.data.utc_time + '

'; } else { output.innerHTML = '

Error: ' + data.data.message + '

'; } }) .catch(function(error) { output.innerHTML = '

Connection Error: ' + error.message + '

'; }); }); });

 

Result

  1. Add the shortcode [show_time_button] to any post or page.
  2. When the page loads, you will see a button labeled “Show Time.”
  3. Clicking the button will display the local and UTC time using our custom AJAX admin.

With this setup, you can handle AJAX requests more efficiently and lightly in WordPress. This approach is ideal for applications where performance is crucial, and only the basic core functionalities are needed.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Back to Top
0
Would love your thoughts, please comment.x
()
x