WordPress and WooCommerce Code Snippets
These are the WordPress and WooCommerce code snippets I keep coming back to. Where a snippet has its own post, the post explains it and was tested again in November 2026 on WordPress 7.1.4, WooCommerce 11.2.2 and PHP 8.4. The rest live as GitHub Gists, so test them on a staging site before you use them on a live one.
Add PHP snippets to a child theme’s functions.php or a small plugin, not to a parent theme you update. All of them are on GitHub (opens in a new tab) under the GPL-3.0 licence, and the posts are also listed in Code Snippets.
WooCommerce Subscriptions
These need the WooCommerce Subscriptions extension.
- Custom failed-payment retry rules (opens in a new tab)
- Add the parent order ID to renewal orders (opens in a new tab)
- Webhook topics for subscription status changes (opens in a new tab)
Checkout, shipping and accounts
- Add Bootstrap classes to WooCommerce checkout fields (post, tested November 2026)
- Remove links from the WooCommerce My Account menu (post, tested November 2026)
- Hide other shipping rates when free shipping is available (opens in a new tab)
- Change WooCommerce coupon labels (opens in a new tab)
- Log users out and redirect after registration (opens in a new tab)
- Redirect users after they log out of WooCommerce (opens in a new tab)
Products and the shop
- Remove decimals from WooCommerce product prices (post, tested November 2026) · gist (opens in a new tab)
- Get the URLs of all WooCommerce pages (post, tested November 2026) · gist (opens in a new tab)
- Related products from the Yoast primary category (post, tested November 2026) · gist (opens in a new tab)
- Exclude categories from the Shop page (opens in a new tab)
- Show a single category in the product loop (opens in a new tab)
- List products by category on a category page (opens in a new tab)
- List products by category on an archive page (opens in a new tab)
- Search admin orders by custom meta fields (opens in a new tab)
Security and server
- Save FTP details in wp-config.php (post, tested November 2026) · gist (opens in a new tab)
- Enable direct file access (FS_METHOD) (opens in a new tab)
- Use a secure FTP connection (opens in a new tab)
- Require authentication for the REST API (opens in a new tab)
Admin and users
- Reorder the admin sidebar (post, tested November 2026) · gist (opens in a new tab)
- Hide admin sidebar links (opens in a new tab)
- Rename admin sidebar links (opens in a new tab)
- Show custom post types in the At a Glance box (post, tested November 2026) · gist (opens in a new tab)
- Use first and last name as the display name (opens in a new tab)
- Allow draft and private pages as parent pages (opens in a new tab)
- Print a PHP array in the browser console (opens in a new tab)
Search, taxonomies and post types
- Include custom post types in search results (post, tested November 2026)
- Include ACF fields in WordPress search (opens in a new tab)
- A shortcode for the search form (opens in a new tab)
- Disable taxonomy archives (post, tested November 2026) · gist (opens in a new tab)
- Add custom post types to tag and category archives (opens in a new tab)
- Set a default term for a custom taxonomy (opens in a new tab)
- Custom image sizes for custom post types (opens in a new tab)
- Different excerpt lengths for different post types (post, tested November 2026) · gist (opens in a new tab)
- Add a Read More link to excerpts (post, tested November 2026)
- Move the comment field below the name fields (post, tested November 2026) · gist (opens in a new tab)
- Hide categories from the category widget (post, tested November 2026) · gist (opens in a new tab)
- List a post’s categories with links (opens in a new tab)
- Related posts that share a tag (opens in a new tab)
- Count post views (opens in a new tab)
- Change the URL base of blog posts (opens in a new tab)
SEO and Yoast
- Disable Yoast SEO’s automatic redirects (opens in a new tab)
- A shortcode for Yoast’s reading time (opens in a new tab)
- Fill image alt text from media metadata (opens in a new tab)
Older snippets
These are for older tools such as Bootstrap 3, WOW.js, Visual Composer and OpenCart. I keep them for reference, but I don’t maintain them.
Show older snippets
- Five equal columns in Bootstrap 3 (post, tested November 2026) · gist (opens in a new tab)
- Keep a Bootstrap 3 dropdown open on inside clicks (opens in a new tab)
- Add a browser-specific class to the body (opens in a new tab)
- Turn off WOW.js on mobile (opens in a new tab)
- Turn off Visual Composer animations on mobile (opens in a new tab)
- Use Pardot with Formidable Forms (opens in a new tab)
- Attach a PDF to Contact Form 7 emails (opens in a new tab)
- 301 redirects in OpenCart (opens in a new tab)
I used AI to help draft this post from my own idea and notes. I verified the information and edited the text myself.