Head Sorter - Intelligent reorganization of the <head> to boost speed and SEO
For many WordPress sites, the order of tags in the <head> is silent chaos: metadata drowns out, CSS arrives too late, scripts block... The result: slower rendering, lower Web Vitals metrics and poorer SEO. The module Head Sorter from WPMasterToolKit has been created to put things in order automatically - without complicated settings - and put what really matters first.
Which plug-in can the "Head Sorter" module replace?
In many cases, Head Sorter can replace small plugins dedicated to "head tag sorting", load order optimizations, or in-house snippets that attempt to manually move <meta>, <link> and <script>. Here, everything is managed systematically and securely via an HTML parser.
How it works (in plain English)
- Intercepting front-end rendering
The module attaches totemplate_redirect(priority -1000) to start a output buffer (output buffering). It does not that the Front-end. - HTML detection
If the page does not contain</html>the content remains unchanged. - Analysis of
<head>with DOMDocument
The contents of the<head>is loaded into a DOMDocument (with silent error handling), then all child tags are evaluated and sorted according to a weight. - Sort by importance
Nodes (tags, text, comments) are assigned a weight. We re-inserts then the elements in the<head>from most to least important, in preserving comments and texts.
The priority grid (from most to least important)
- META (10) :
charset,viewportand somehttp-equivreviews (content-security-policy,content-type,default-styleetc.) - TITLE (9)
- PRECONNECT (8) :
<link rel="preconnect"> - ASYNC_SCRIPT (7) :
<script src ... async> - IMPORT_STYLES (6) :
<style>containing@import - SYNC_SCRIPT (5) :
<script>inline blocking (nondefernoasyncnotype="module"and notypeJSON) - SYNC_STYLES (4) :
<link rel="stylesheet">and<style> - PRELOAD (3) :
<link rel="preload|modulepreload"> - DEFER_SCRIPT (2) :
<script src ... defer>or<script type="module">(withoutasync) - PREFETCH_PRERENDER (1) :
<link rel="prefetch|dns-prefetch|prerender"> - OTHER (0): everything else, including JSON scripts (
application/ld+jsonetc.) and non-element nodes (texts, comments), which are kept
👉 In concrete terms, your critical tags (charset, viewport, title) go to the topresources, followed by connection and CSS necessary for rendering, and blocking scripts are eliminated as far as possible by giving priority to async/defer/module.
Why it's good for performance and SEO
- Faster FCP/LCP essential meta and styles arrive early, reducing initial rendering time.
- Less blocking JS scripts
async/defer/moduleare favored, reducing the impact on the critical rendering path. - Technical SEO one
<head>better structured helps engines to understand and index correctly (consistent title/meta first).
Behaviors and compatibility to know
- Front-end only : hooked on
template_redirect. The admin is not impacted. - Content preservation comments and texts in the
<head>are retained. - JSON scripts (e.g.
application/ld+json) : not treated as blocking scripts ("OTHER" weight) to avoid moving them aggressively. - ES modules (
type="module"): treated as defer by default (good for rendering). - Ill-formed HTML DOMDocument can attempt to "correct" a badly structured head, but keep HTML clean for best results.
How to use this module
- Activate the Head Sorter module in WPMasterToolKit.
- Empty cache your plugins/server/CDN if you use them.
- Control a page (home page, sample article) to check the order of the
<head>with the browser's dev tools. - No adjustments to make: everything is automatic.
Our technical choices (and why)
- Output buffering at the earliest (
template_redirectpriority -1000) to capture the final rendering of the page without touching the heart of WordPress or the themes. - DOMDocument for a reliable handling rather than fragile regexes.
- Weight matrix clear and extensible code to prioritize :
- Meta/title first and foremost (web standards + SEO).
- Preconnect/Preload/Style sheets before blocking scripts.
async/defer/modulebetter off than conventional synchronous scripts.
- Conservatism on JSON : exclusion of
typecontainingjsonfrom the "synchronous script" category to avoid affecting the markup of structured data.
Hooks and filters
This module exposes no apply_filters or do_action personalized. Behavior is entirely internal.
Practical tips
- Avoid
@importin your<style>: even if they have priority here, they remain slower that a<link rel="stylesheet">. - If a critical script needs to run early, prefer
defer(ortype="module") rather than synchronous, so as not to block rendering. - Use
rel="preconnect"andrel="preload"with parsimony and precision (preload only what is critical).
Conclusion
The module Head Sorter brings order to the <head> of your WordPress site automatically, respecting good performance and technical SEO practices. Activate it, clear your caches, and enjoy cleaner, faster loading - without wasting time micromanaging tags.