WordPress code snippets
Hooks, shortcodes and theme helpers (GPL). 28 free snippets, each with an explanation and usage notes.
- 28
- snippets
- 2
- types
All snippets
WordPress · InternationalisationPOT generation script with WP-CLIA shell script that extracts translatable strings to a .pot file, merges them into .po files and compiles .mo and JSON files.i18npotwp-cli WordPress · DatabaseCustom post type with a linked taxonomyRegister a bookable-room post type with a hierarchical amenity taxonomy, REST support and a safe rewrite flush on theme switch.cpttaxonomyrewrite WordPress · ThemingDark and light mode toggle without flashA theme-level colour mode switch that respects the visitor's system setting, remembers a choice and avoids a flash of the wrong theme.dark-modetoggleaccessibility WordPress · Themingtheme.json colour, type and spacing presetsA compact theme.json that defines a palette, fluid font sizes, a spacing scale and element styles for the block editor.theme-jsonblock-themedesign-tokens WordPress · UtilitiesSticky mini audio player dockA fixed bottom audio dock that plays any link marked with data-audio and keeps playing while the visitor browses the page.audioplayerpodcast WordPress · FormsSettings API options pageA complete options page using register_setting, sections and fields with sanitise callbacks and a capability gate.settings-apioptionsadmin WordPress · UtilitiesSanitised inline SVG icon helperLoad SVG icons from the theme folder, strip anything unsafe with wp_kses and a strict allow-list, and print them inline.svgiconskses WordPress · APIsREST route with permission callback and schemaRegister a REST API route that validates its arguments, authorises with permission_callback and returns a shaped response.rest-apipermission-callbackvalidation WordPress · UtilitiesRelated posts by shared taxonomy termsQuery the posts that share the most tags with the current one, fall back to its category, and cache the result in a transient.related-postswp-querytaxonomy WordPress · AnimationReduced-motion aware scroll revealFade elements in as they enter the viewport with an IntersectionObserver, staggered delays and a full opt-out for reduced motion.scroll-revealintersection-observerreduced-motion WordPress · UtilitiesReading time and automatic table of contentsFilter the_content to add heading anchors, a linked table of contents and a words-per-minute reading time estimate.the-contenttocreading-time WordPress · PricingMonthly / yearly pricing toggle shortcodeA pricing table shortcode with an accessible monthly and yearly switch driven by data attributes and a few lines of script.pricingshortcodetoggle WordPress · SEOPodcastEpisode structured dataOutput valid PodcastEpisode JSON-LD in the head of single episode pages, built from post data and safely encoded.json-ldschema-orgpodcast WordPress · APIsNonce and capability checked AJAX endpointA WordPress admin AJAX action that verifies a nonce, checks a capability, sanitises input and answers with JSON.ajaxnoncecapability WordPress · Feedback and empty statesLive blog with polling auto-refreshA REST endpoint returns entries newer than a timestamp and a small script polls it, pausing while the tab is hidden.live-blogrest-apipolling WordPress · UtilitiesLazy-loading YouTube facadeShow a thumbnail and play button instead of an iframe, and swap in the real embed only when the visitor clicks.youtubelazy-loadingperformance WordPress · FormsBooking bar form with date validationA date and guests booking bar shortcode with a nonce, server-side validation and a redirect to a results page.bookingformadmin-post WordPress · LayoutsUpcoming events archive with month gridOrder an event archive by date, hide past events and render a month calendar grid that groups events by day.eventscalendarwp-query WordPress · HooksElementor widget skeletonA minimal custom Elementor widget with controls, escaped rendering and the registration hook, ready to extend.elementorwidgetpage-builder WordPress · ThemingDrop cap paragraph block styleRegister a Drop cap style for the paragraph block and style it with a pure CSS first-letter rule that works in editor and front end.block-stylesdrop-captypography WordPress · UtilitiesOne-click demo content importerAn admin tools page that imports pages and posts from a bundled JSON file idempotently, behind a nonce and capability check.importdemo-contentadmin-page WordPress · LayoutsWeekly class timetable gridTurn a flat list of sessions into an accessible day-by-time table with a shortcode, escaped output and empty cell handling.timetableschedulegrid WordPress · ThemingChild theme starterA minimal child theme with a correct stylesheet header and a functions file that loads parent and child styles with cache busting.child-themeenqueuecustomisation WordPress · DatabaseChangelog post type with version meta boxA release-notes post type with a nonce-protected version field, admin columns and a shortcode that lists releases newest first.changelogmeta-boxcpt