Snippet library
PHP snippets for WordPress.
467 free & Pro snippets and modules. Copy-paste-ready, or import in one click with SnipCraft.
Dequeue Scripts and Styles by Page
Conditionally dequeues specified CSS and JS handles on pages where they are not needed, reducing per-page asset weight.
Preload Hero/Featured Image
Outputs a rel='preload' hint for the current post's featured image on singular pages, prioritising its fetch to improve Largest Contentful Paint.
Preload Critical Fonts
Outputs rel='preload' link hints in the document head for self-hosted web-font files so the browser fetches them at the highest priority.
Disable Core Lazy Loading
Removes the native loading='lazy' attribute that WordPress automatically adds to images and iframes, useful when a theme or plugin manages its own lazy loading.
Disable Image Compression (Max Quality)
Forces WordPress to save uploaded images at 100% quality, disabling all lossy compression so originals are preserved exactly.
Set JPEG and WebP Quality
Sets the JPEG and WebP compression quality WordPress uses when processing uploaded images to balance file size and visual fidelity.
Limit Which Image Sizes Generate
Restricts WordPress to generating only a specified allowlist of image sizes on upload, discarding all others to keep the uploads folder lean.
Disable Auto-Generated Image Sizes
Prevents WordPress from generating thumbnail, medium, medium_large, and large derivative files on upload, saving disk space.
Enable All Automatic Updates
Opts the site into automatic background updates for core (including major versions), plugins, themes, and translations.
Disable All Automatic Updates
Disables all WordPress background auto-updates for core, plugins, themes, and translations to prevent unexpected version changes.
Test the Database Connection
Adds a Tools submenu page that runs diagnostic queries (SELECT 1, row counts, DB version) and reports the results.
Clear All Transients on Demand
Adds a Tools submenu page with a button that deletes every transient from the options table in a single query.
Search and Replace Content Output
Substitutes one or more strings in post content and widget text on output, without touching the database.
Enable Debug Logging via Snippet
Activates PHP error logging to wp-content/debug.log at runtime; ensures the file exists with safe permissions. Disable when debugging is complete.
Log 404 Errors to Database
Records each unique 404 URL, its referrer, and hit count to a custom database table so broken links can be identified and fixed.
Redirect All 404s to Homepage
Issues a 301 permanent redirect to the homepage for every 404 Not Found response, catching broken inbound links.
Increase Max Execution Time and Upload Size
Raises PHP time limits and upload size caps at runtime, giving long-running imports and large file uploads the headroom they need.
Increase PHP Memory Limit
Raises the PHP memory limit to 512 MB for WordPress requests, useful when heavyweight plugins exhaust the default allocation.
Maintenance Mode for Non-Admins
Displays a 503 maintenance page to all non-administrator visitors while leaving the wp-admin dashboard accessible.
Notify Admin When New Admin Created
Emails the site administrator when any user account is granted the administrator role, to detect unexpected privilege escalation.
Alert Admin When Plugin Activated
Sends the site administrator an email with the plugin name, activating user, and timestamp whenever a plugin is activated.
Add Comment Form Honeypot
Inserts a hidden form field into the comment form that only bots fill in; any submission with a filled honeypot is silently rejected.
Restrict Registration Email Domains
Blocks sign-ups from a list of known disposable and throwaway email providers to reduce spam accounts.
Block Bad Bots by User Agent
Denies requests from known scraper and harvester user agents with a 403 response.