Content Marketing

WordPress: How To Update Admin Menu Links with jQuery to Sort Results Automatically (with Rank Math Example)

One of the WordPress plugins that I install on every one of my sites or my clients’ sites is Rank Math. When launching a site, one critical step is monitoring and correcting any visits to pages not found (404 errors). The plugin’s 404 monitor is a fantastic tool that needs to be enabled in the plugin settings. If your SEO consultant isn’t analyzing backlinks and monitoring 404 errors, find a new one!

Keep in mind that not all 404s are valid. Many are produced by bots that are crawling your site for vulnerabilities. However, many are valid. You should create content if the page doesn’t exist or redirect to the user to a relevant page if it does exist. I tend to monitor the report for the 404s most accessed (by sorting the report by Hits descending) and then analyzing the top results.

Rank Math 404 Monitor

The Rank Math 404 Monitor is simple, providing the URL that resulted in a 404, the number of times it was hit, and the last time it was accessed. By default, the report is sorted by the access time descending which is really quite useless.

rank math 404 report

Because I’m in this report so much, it’s both a pain and taxing to sort the report manually. I have to open the page, click Hits and it sorts ascending, then click Hits again to sort it descending. I want to open the report and, by default, have it sorted descending by the number of hits. Unfortunately, there’s no setting for this in the plugin.

How To Update Menu Links To Sort

Because I didn’t develop this plugin, I don’t want to make any edits to it since it’s updated often with new features. However, I can utilize jQuery to update the links as soon as the page is loaded… which doesn’t change the plugin at all. There are two menu locations for this link… the typical admin menu on the left and a shortcut menu in the header of the admin page. I want to update both.

Within my theme’s functions.php page, I added the following code:

function sort_rank_math_404_footer_script() {
    // Check if the admin bar is showing and only then print the script.
    if (is_admin_bar_showing()) {
        ?>
        <script type="text/javascript">
            jQuery(document).ready(function($) {
                // Selector for the 404 Monitor link in the admin menu and admin bar
                var adminMenuSelector = 'li a[href="admin.php?page=rank-math-404-monitor"]';
                var adminBarSelector = '#wp-admin-bar-rank-math-404-monitor a';

                // Function to append query parameters to href attributes
                function appendQueryParameters(selector, query) {
                    $(selector).each(function() {
                        var href = $(this).attr('href');
                        // Ensure the link is not already modified
                        if (href.indexOf('orderby=times_accessed') === -1) {
                            $(this).attr('href', href + query);
                        }
                    });
                }

                // Append query parameters to the admin menu and admin bar links
                appendQueryParameters(adminMenuSelector, '&orderby=times_accessed&order=desc');
                appendQueryParameters(adminBarSelector, '&orderby=times_accessed&order=desc');
            });
        </script>
        <?php
    }
}

add_action('admin_footer', 'sort_rank_math_404_footer_script');

This script function checks if the admin bar is being displayed and then runs a JavaScript snippet to append the query parameters to the href attributes of both the admin menu and admin bar links.

&orderby=times_accessed&order=desc

It checks to ensure it’s not appending the query string multiple times in case the script runs more than once. Now, when I open WordPress Admin, each of the menu links is updated with the querystring to sort the list of 404s by hits descending by appending the querystring to the link. When I click on the report link, my report is sorted the way I want it to be:

rank math 404 sort descending

Appreciate this content?

Sign up for our weekly newsletter, which delivers our latest posts every Monday morning.

We don’t spam! Read our privacy policy for more info.

Douglas Karr

Douglas Karr is CMO of OpenINSIGHTS and the founder of the Martech Zone. Douglas has helped dozens of successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to assist companies in implementing and automating their sales and marketing strategies. Douglas is an internationally recognized digital transformation and MarTech expert and speaker. Douglas is also a published author of a Dummie's guide and a business leadership book.
Back to top button
Close

Adblock Detected

Martech Zone is able to provide you this content at no cost because we monetize our site through ad revenue, affiliate links, and sponsorships. We would appreciate if you would remove your ad blocker as you view our site.