Ecommerce Shopify WordPress Discussion

How to modify the core WordPress admin menu to load via AJAX?

I cannot find any plugin that does this, nor any thread with someone asking this, so it's either very difficult or very simple. Note: This is not for ajax loading panel/tabs menus within an admin page, this is for the entire WP backend admin menu, regardless of speed and potential issues. Menu Items such as: Dashboard, Pages, Posts, Users, etc. I asked GPT3.5 and this is what I got, could someone confirm if this would be the right way to do it? Enqueue jQuery in your theme or plugin: Ensure that jQuery is enqueued in your theme or plugin. WordPress includes jQuery by default, but you might want to check and enqueue it explicitly if needed. phpCopy code function enqueue_custom_scripts() { wp_enqueue_script('jquery'); } add_action('wp_enqueue_scripts', 'enqueue_custom_scripts'); Create a JavaScript file for your AJAX functionality: Create a JavaScript file (e.g., custom-admin-ajax.js) and enqueue it in your theme or plugin. This file will contain your AJAX logic. javascriptCopy code jQuery(document).ready(function($) { // AJAX request to load WordPress admin menu $.ajax({ url: ajaxurl, data: { action: 'load_admin_menu', }, success: function(response) { // Update the menu with the response $('#adminmenuwrap').html(response); } }); }); Create a PHP function to handle the AJAX request: In your theme's functions.php file or in your custom plugin, add a PHP function to handle the AJAX request. phpCopy code function load_admin_menu_callback() { // Your code to generate the admin menu ob_start(); require_once ABSPATH . 'wp-admin/menu-header.php'; // Include WordPress menu header $menu = ob_get_clean(); // Send the generated menu as the AJAX response echo $menu; wp_die(); // Always include this to terminate the script properly } add_action('wp_ajax_load_admin_menu', 'load_admin_menu_callback'); In this example, we use ob_start() to buffer the output of menu-header.php and then send it as the AJAX response. Hook the JavaScript file and localize AJAX URL: Enqueue your JavaScript file and localize the AJAX URL in your theme's functions.php or in your custom plugin. phpCopy code function enqueue_custom_scripts() { wp_enqueue_script('custom-admin-ajax', get_template_directory_uri() . '/js/custom-admin-ajax.js', array('jquery'), null, true); wp_localize_script('custom-admin-ajax', 'ajaxurl', admin_url('admin-ajax.php')); } add_action('wp_enqueue_scripts', 'enqueue_custom_scripts'); The wp_localize_script function is used to pass data from PHP to JavaScript. In this case, we're passing the ajaxurl variable which contains the URL to admin-ajax.php. Now, when the page loads, it will trigger an AJAX request to admin-ajax.php with the action load_admin_menu. The server will respond with the generated admin menu, and the JavaScript will update the menu on the page. I tried to find a Plugin or Theme that would do this for the backend, but Menu Editor Pro doesn't do it, and Adminify only ajax load the sub-menu items, but reloads the page when clicked.
Yes, that should work, may need a tweak but you'll need to run it. ob_start function allows you to load a PHP file but not include it in any output, as it gets buffered. ob_end_clean function gets that buffers contents, so that line puts the content into a variable. You might just want to find where it normally loads in the template and comment it out/replace it with your response. In general it looks good.

February 4, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.