Plugin Directory

Changeset 3505977


Ignore:
Timestamp:
04/14/2026 11:09:34 AM (6 months ago)
Author:
vaibhav31gangrade
Message:

Fixed inner menus navigation and active class issue.

Location:
ajax-admin-navigation-sidebar-menus-by-ajax/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • ajax-admin-navigation-sidebar-menus-by-ajax/trunk/ajax-admin-navigation-sidebar-menus-by-ajax.php

    r3215562 r3505977  
    11<?php
    22/*
    3 Plugin Name: AJAX Admin Navigation Sidebar AJAX
     3Plugin Name: AJAX Admin Navigation Sidebar
    44Description: Adds AJAX navigation to the WordPress admin dashboard to prevent full page reloads.
    5 Version: 1.0
     5Version: 1.1
    66Author: Vaibhav Gangrade
    7 Author URI:
    8 Stable Version: 1.0
    9 Tested Up To: 6.6
    107License: GPL v2 or later
    118License URI: https://www.gnu.org/licenses/gpl-2.0.html
    12 Short Description: Plugin for controlling admin menus sideabr with ajax for prevnting extra server load.
    139*/
    14 
    1510
    1611if (!defined('ABSPATH')) {
    … …  
    2116    class AJAX_Admin_Navigation_Menus {
    2217
    23         // Calling constructor
    2418        public function __construct() {
    2519            add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
    … …  
    2721        }
    2822
    29         // Enqueueing script files
     23        // Enqueue scripts and styles
    3024        public function enqueue_scripts() {
    31             wp_enqueue_script('custom-admin-ajax', plugin_dir_url(__FILE__) . 'js/custom-admin-ajax.js', array('jquery'), null, true);
     25            wp_enqueue_script(
     26                'custom-admin-ajax',
     27                plugin_dir_url(__FILE__) . 'js/custom-admin-ajax.js',
     28                array('jquery'),
     29                null,
     30                true
     31            );
    3232
    33            wp_localize_script('custom-admin-ajax', 'ajax_admin_nav_object', array(
     33            wp_localize_script('custom-admin-ajax', 'ajax_admin_nav_object', array(
    3434                'ajax_url' => admin_url('admin-ajax.php'),
    3535                'nonce'    => wp_create_nonce('custom-admin-ajax-nonce'),
    3636            ));
    3737
    38 
    39             // Enqueue the custom CSS file
    40             wp_enqueue_style('custom-admin-style', plugin_dir_url(__FILE__) . 'css/style.css');
     38            wp_enqueue_style(
     39                'custom-admin-style',
     40                plugin_dir_url(__FILE__) . 'css/style.css'
     41            );
    4142        }
    4243
     44        // Load admin assets during AJAX requests
    4345        public function load_admin_assets() {
    4446            if (defined('DOING_AJAX') && DOING_AJAX) {
    45                 // Enqueue WordPress admin scripts for navigation and content management
    46                 wp_enqueue_script('post');  // For post/page editing screens
    47                 wp_enqueue_script('editor'); // The editor script (TinyMCE)
    48                 wp_enqueue_script('nav-menu'); // Navigation menus management
    49                 wp_enqueue_script('media'); // Media manager (for uploading images and files)
    50                 wp_enqueue_script('link'); // Internal linking feature
    51                 wp_enqueue_script('wplink'); // WordPress link editor
    52                 wp_enqueue_script('wp-color-picker'); // Color picker tool
    53                 wp_enqueue_script('customize-controls'); // Customizer controls
    54                 wp_enqueue_script('dashboard'); // Dashboard widgets
    55                 wp_enqueue_script('wp-api'); // REST API integration
    56                 wp_enqueue_script('autosave'); // Autosave feature for posts/pages
    57                 wp_enqueue_script('media-views'); // Media views for the media library
    58                 wp_enqueue_script('tags-box'); // Tag and category boxes
    59                 wp_enqueue_script('wp-block-editor'); // Gutenberg block editor (if needed)
     47                wp_enqueue_script('post');
     48                wp_enqueue_script('editor');
     49                wp_enqueue_script('nav-menu');
     50                wp_enqueue_script('media');
     51                wp_enqueue_script('link');
     52                wp_enqueue_script('wplink');
     53                wp_enqueue_script('wp-color-picker');
     54                wp_enqueue_script('customize-controls');
     55                wp_enqueue_script('dashboard');
     56                wp_enqueue_script('wp-api');
     57                wp_enqueue_script('autosave');
     58                wp_enqueue_script('media-views');
     59                wp_enqueue_script('tags-box');
     60                wp_enqueue_script('wp-block-editor');
    6061
    61                 // Enqueue WordPress admin styles for navigation and content management
    62                 wp_enqueue_style('editor-buttons'); // Styles for editor buttons
    63                 wp_enqueue_style('wp-jquery-ui-dialog'); // jQuery UI dialog styles
    64                 wp_enqueue_style('nav-menu'); // Navigation menus styles
    65                 wp_enqueue_style('wp-color-picker'); // Color picker styles
    66                 wp_enqueue_style('customize-controls'); // Customizer controls styles
    67                 wp_enqueue_style('dashboard'); // Dashboard widgets styles
    68                 wp_enqueue_style('media-views'); // Styles for media views
    69                 wp_enqueue_style('wp-block-editor'); // Styles for Gutenberg block editor (if needed)
    70                
    71                 // Add more scripts or styles if needed
     62                wp_enqueue_style('editor-buttons');
     63                wp_enqueue_style('wp-jquery-ui-dialog');
     64                wp_enqueue_style('nav-menu');
     65                wp_enqueue_style('wp-color-picker');
     66                wp_enqueue_style('customize-controls');
     67                wp_enqueue_style('dashboard');
     68                wp_enqueue_style('media-views');
     69                wp_enqueue_style('wp-block-editor');
    7270            }
    7371        }
    74 
    75 
    76 
    77 
    78     }//end of class
     72    }
    7973}
    8074
    8175new AJAX_Admin_Navigation_Menus();
    82 
    83 
  • ajax-admin-navigation-sidebar-menus-by-ajax/trunk/js/custom-admin-ajax.js

    r3215562 r3505977  
    11jQuery(document).ready(function($) {
    2     // Handle menu item clicks
    3     $('#adminmenu a').on('click', function(e) {
    4         e.preventDefault();
    52
    6         var url = $(this).attr('href');
    7 
    8         // Show the full-page overlay with spinner
     3    // Centralized AJAX loader
     4    function loadAjaxContent(url, $link) {
    95        $('body').append('<div class="full-page-overlay"><div class="spinner"></div></div>');
    106
    11         // Load the content via AJAX
    127        $.ajax({
    138            url: url,
    149            type: 'GET',
    1510            success: function(response) {
    16                 // Extract the content from the response
    17                 var content = $(response).find('#wpbody-content').html();
    18                 $('#wpbody-content').html(content);
     11            var content = $(response).find('#wpbody-content').html();
     12            $('#wpbody-content').html(content);
    1913
    20                 // Update the browser's URL without reloading the page
    21                 history.pushState(null, null, url);
     14            history.pushState(null, null, url);
    2215
    23                 // Reinitialize scripts if needed (e.g., for forms, buttons, etc.)
    24                 $(document).trigger('ajaxPageLoad');
     16            // Trigger reinit
     17            $(document).trigger('ajaxPageLoad');
    2518
    26                 // Special handling for post-new.php
    27                 if (url.indexOf('post-new.php') !== -1) {
    28                     initializePostEditor();
    29                 }
     19            if (url.indexOf('post-new.php') !== -1) {
     20                initializePostEditor();
     21            }
    3022
    31                 // Remove the full-page overlay with spinner
    32                 $('.full-page-overlay').remove();
    33             },
     23            // ✅ Update active class in sidebar
     24            $('#adminmenu li').removeClass('current wp-has-current-submenu');
     25            if ($link && $link.closest('li').length) {
     26                $link.closest('li').addClass('current wp-has-current-submenu');
     27                // Also mark parent if it’s a submenu
     28                $link.closest('li.wp-has-submenu').addClass('wp-has-current-submenu');
     29            }
     30
     31            $('.full-page-overlay').remove();
     32        },
    3433            error: function() {
    3534                alert('Failed to load content.');
    36 
    37                 // Remove the full-page overlay with spinner
    3835                $('.full-page-overlay').remove();
    3936            }
    4037        });
     38    }
    4139
    42         return false;
     40    // Handle clicks on admin menu + inner links
     41    $(document).on('click', '#adminmenu a, .wrap a', function(e) {
     42        var url = $(this).attr('href');
     43
     44        // Skip external links or anchors
     45        if (!url || url.startsWith('#') || $(this).attr('target') === '_blank') {
     46            return;
     47        }
     48
     49        e.preventDefault();
     50        loadAjaxContent(url, $(this));
    4351    });
    4452
    45     // Handle browser back/forward buttons
     53    // Handle browser back/forward
    4654    $(window).on('popstate', function() {
    4755        var url = window.location.href;
    48 
    49         // Show the full-page overlay with spinner
    50         $('body').append('<div class="full-page-overlay"><div class="spinner"></div></div>');
    51 
    52         // Load the content via AJAX
    53         $.ajax({
    54             url: url,
    55             type: 'GET',
    56             success: function(response) {
    57                 var content = $(response).find('#wpbody-content').html();
    58                 $('#wpbody-content').html(content);
    59 
    60                 // Reinitialize scripts if needed
    61                 $(document).trigger('ajaxPageLoad');
    62 
    63                 // Special handling for post-new.php
    64                 if (url.indexOf('post-new.php') !== -1) {
    65                     initializePostEditor();
    66                 }
    67 
    68                 // Remove the full-page overlay with spinner
    69                 $('.full-page-overlay').remove();
    70             },
    71             error: function() {
    72                 alert('Failed to load content.');
    73 
    74                 // Remove the full-page overlay with spinner
    75                 $('.full-page-overlay').remove();
    76             }
    77         });
     56        loadAjaxContent(url, null);
    7857    });
    7958
    80     // Function to reinitialize the post editor
     59    // Reinitialize post editor
    8160    function initializePostEditor() {
    82         // Reinitialize the WordPress editor components for post-new.php
    8361        if (typeof tinymce !== 'undefined') {
    84             tinymce.init({
    85                 selector: '#content', // Ensure the correct selector is used
    86                 // Add other TinyMCE options as needed
    87             });
     62            tinymce.init({ selector: '#content' });
    8863        }
    89 
    90         // Reinitialize other scripts for post editor, e.g., Quicktags, media, etc.
    9164        if (typeof quicktags !== 'undefined') {
    9265            quicktags({id: 'content'});
    … …  
    9568            wp.media.editor.initialize('content');
    9669        }
    97 
    98         // Trigger any other required WordPress actions
    9970        $(document).trigger('wp-editor-reinit');
    10071    }
     72
     73    // Re-bind WP list table scripts after AJAX load
     74    $(document).on('ajaxPageLoad', function() {
     75        if (typeof listTable !== 'undefined' && listTable.init) {
     76            listTable.init();
     77        }
     78    });
    10179});
    102 
    103 
    104 
    105  
  • ajax-admin-navigation-sidebar-menus-by-ajax/trunk/readme.txt

    r3215562 r3505977  
    33Tags: AJAX Navigation, Navigation with AJAX, AJAX Menu, AJAX Menu Plugin 
    44Requires at least: 4.6 
    5 Tested up to: 6.7 
     5Tested up to: 6.9 
    66Stable tag: 1.0 
    77Requires PHP: 5.2.4 
    … …  
    4949= 1.1 =
    5050* Fixed a security-related bug. Upgrade immediately.
     51* Fixed loading post and pages inner menus
    5152
    5253= 1.0 =
Note: See TracChangeset for help on using the changeset viewer.