Snippet library
PHP snippets for WordPress.
467 free & Pro snippets and modules. Copy-paste-ready, or import in one click with SnipCraft.
Email Admin After Repeated Failed Logins
Tracks failed login attempts per IP using transients and emails the site admin when a configurable attempt threshold is crossed within a rolling window.
Add Google reCAPTCHA v3 to the Login Form
Integrates Google reCAPTCHA v3 into wp-login.php to silently score submissions and block likely-bot login attempts.
Remove REST API Link Tag from Head
Removes the REST API discovery link from the HTML head and HTTP Link header to reduce information exposure to scanners.
Block Comments Containing Links
Automatically marks as spam any new comment that contains a URL, reducing link-spam without requiring CAPTCHA.
Allow Login by Email Address Only
Restricts the WordPress login form to email addresses only, rejecting plain username attempts with a friendly error message.
Disable Login with Email Address (Username Only)
Rejects login attempts that use an email address, forcing users to enter their WordPress username instead.
Disable PHP Error Display on the Front End
Suppresses PHP error, warning, and notice output on the public site so internal details are never leaked to visitors.
Block Visitors by IP Address
Denies access at the WordPress init stage to any visitor whose IP matches a configurable blocklist, returning a 403 Forbidden response.
Limit WooCommerce Action Scheduler Retention
Reduces how long Action Scheduler retains completed and failed actions in the database, keeping the table small and cleanup queries fast.
Set oEmbed Maximum Width
Caps the maximum width of embedded media such as YouTube and Vimeo via the embed_defaults filter to fit your content column.
Set fetchpriority=high on the Hero Image
Adds fetchpriority="high" and removes lazy-loading from the post thumbnail on singular views to boost Largest Contentful Paint.
Lazy-Load Gravatars in Comments
Adds the native loading="lazy" attribute to all Gravatar img tags in the comments section to defer off-screen avatar network requests.
Disable Dashicons for Logged-Out Visitors
Dequeues the Dashicons stylesheet on the public front end for visitors who are not logged in, saving an HTTP request and ~30 KB.
Disable WooCommerce Cart Fragments AJAX
Dequeues the wc-cart-fragments script that fires an admin-ajax.php request on every page load, improving time to first byte on WooCommerce stores.
Self-Host Google Fonts Locally
Dequeues Google Fonts requests added by themes or plugins and enqueues a self-hosted fonts.css from the active theme instead.
Defer Non-Critical CSS Loading
Converts specified stylesheet handles to rel="preload" links with an onload swap, removing them from the render-blocking path.
Minify HTML Output
Strips unnecessary whitespace and blank lines from the HTML response to reduce page size, preserving pre/script/style/textarea content verbatim.
Disable WP-Cron and Use a Server Cron
Prevents WordPress from spawning its built-in cron on every page load and includes setup notes for a real server cron job.
Add a Custom Field Column to a Post List
Adds a sortable meta-value column to the admin post list table for any post type and meta key.
Display Current Post's Taxonomy Terms via Shortcode
Shortcode [post_terms taxonomy="category"] lists the terms of any taxonomy assigned to the current post.
Make a Taxonomy Single-Select with Radio Buttons
Replaces the default taxonomy checkbox meta box with a radio-button list that enforces single-term selection.
Register a Custom Post Status
Registers an 'Archived' post status for posts and pages, and adds it to the Classic Editor status dropdown.
Add rel="sponsored" to Affiliate Links
Rewrites outbound links with the CSS class 'affiliate' or matching configured domains to carry rel="sponsored noopener".
Display Post Word Count Shortcode
Shortcode [word_count] displays the total word count of the current or any specified post.