Table of Contents
Here are some simple and practical code snippets you can use on WordPress sites. These are small additions, but they can save time and reduce manual work, especially on content-heavy sites.
Before you start, a few quick notes on where to put things:
- PHP snippets should be added to a child theme or a plugin that supports custom snippets.
- CSS rules can be placed in the child theme style.css, the WordPress Customizer, or a snippets plugin.
- JavaScript code can be added via a child theme or a snippets plugin that supports JS.
If you are not comfortable editing theme files directly, using a snippets plugin is usually the safest option.
Current Year Shortcode
On many sites you see titles like "Best Online Casinos 2026" or "Top UK Bookmakers 2026". If you have dozens or hundreds of pages like this, updating them every year becomes annoying and easy to forget.
A shortcode solves this problem. You register it once and reuse it everywhere.
add_shortcode('current_year', fn () => date('Y'));
add_filter('the_title', 'do_shortcode', 15);
This registers the shortcode and allows it to work inside post titles (H1).
If you want to use it in the browser page title and you are not using any SEO plugins, add this as well:
add_filter('document_title_parts', function($title_parts) {
if (isset($title_parts['title'])) {
$title_parts['title'] = do_shortcode($title_parts['title']);
}
return $title_parts;
});
If you are using an SEO plugin like Yoast or RankMath, enable shortcode support in titles using one of the following:
// Yoast
add_filter('wpseo_title', 'do_shortcode');
// RankMath
add_filter('rank_math/frontend/title', 'do_shortcode');
You can now use [current_year] in page content or titles, and it will always show the current year automatically.
Reading Time Shortcode
This shortcode shows an estimated reading time at the top of a post, for example "3 min read". It is simple, but it helps set expectations for visitors.
add_shortcode('reading_time', function() {
$content = get_post_field('post_content', get_the_ID());
$word_count = str_word_count(strip_tags($content));
if ($word_count < 1) {
return '1 min read';
}
$reading_time = ceil($word_count / 200);
return '<span class="reading-time">'.$reading_time.' min read</span>';
});
You can style it to make it stand out a bit. In this example, it uses a light blue background, a border, and a small clock icon.
.reading-time {
display: inline-flex;
align-items: center;
padding: 2px 8px;
font-size: 12px;
color: #0056b3;
background-color: #f0f7ff;
border: 1px solid #d0e3ff;
border-radius: 4px;
&:before {
content: "⏲️";
margin-right: 6px;
}
}
Use it in posts like this: [reading_time].
Similar logic is built into Akurai Theme, which you can see in action on live demo.
Published Date Shortcode
Most modern themes already show the published date, but if yours does not, this shortcode is a quick fix. Instead of showing a fixed date like "12 September 2025", it displays a relative time such as "4 months ago".
add_shortcode('post_published', function() {
$post_time = get_the_time('U');
return '<time class="entry-date published post-published" datetime="'.get_the_time('c').'">'
.human_time_diff($post_time).' ago
</time>';
});
You can style it however you like. Here is a simple example with the same blue style and a calendar icon.
.post-published {
display: inline-flex;
align-items: center;
padding: 2px 8px;
font-size: 12px;
color: #0056b3;
background-color: #f0f7ff;
border: 1px solid #d0e3ff;
border-radius: 4px;
&:before {
content: "🗓️";
display: inline-block;
margin-right: 6px;
}
}
Use it in posts like this: [post_published].
Last Updated Date Shortcode
For reviews and post articles, showing the last updated date builds trust. This shortcode displays a relative "Last Updated" label, but only if the post was updated at least 24 hours after it was published.
add_shortcode('post_last_updated', function() {
$u_time = get_the_modified_time('U');
$p_time = get_the_time('U');
// Only show if modified at least 24 hours after publishing
if ($u_time >= $p_time + 24 * 60 * 60) {
$updated_date = get_the_modified_date('U');
return '<time class="entry-date updated post-last-updated" datetime="'.get_the_time('c').'">
Last Updated: '.human_time_diff($updated_date).' ago
</time>';
}
return '';
});
You can reuse the same visual style for consistency. In this example, a checkmark icon is added.
.post-last-updated {
display: inline-flex;
align-items: center;
padding: 2px 8px;
font-size: 12px;
color: #0056b3;
background-color: #f0f7ff;
border: 1px solid #d0e3ff;
border-radius: 4px;
&:before {
content: "☑️";
display: inline-block;
margin-right: 6px;
}
}
Use it in posts like this: [post_last_updated].
Copy Bonus Code
Many casino or deal themes allow users to copy a bonus or promo code with one click. If your current setup does not support this, you can add it yourself.
This snippet copies the text when the visitor clicks on it, changes the text to "Copied!", and then switches it back after 2 seconds.
Replace .promo-code with the selector used on your site.
document.querySelectorAll(".promo-code").forEach((wrapper) => {
wrapper.addEventListener("click", async () => {
const code = wrapper.innerText;
try {
await navigator.clipboard.writeText(code);
wrapper.innerText = "Copied!";
wrapper.classList.add("copied");
setTimeout(() => {
wrapper.innerText = code;
wrapper.classList.remove("copied");
}, 2000);
} catch (err) {
console.error("Failed to copy: ", err);
}
});
});
You can optionally style the copied state to give visual feedback:
.promo-code {
cursor: pointer;
user-select: all; /* Highlights the text automatically on click */
transition: color 0.2s ease;
}
.promo-code.copied {
color: #28a745; /* Success Green */
}
Tables provided by Kemoku Plugin use a similar approach, check out the demo website for examples.
External Link Icon
This is a small visual hint that shows visitors when a link goes to an external site.
The rule below adds an arrow icon after links that start with https and do not contain your domain.
a[href^="https"]:not([href*="dinomatic.com"])::after {
content: " ↗";
}
If you are using cloaked affiliate links like /visit/, you can target those instead:
a[href*="/visit/"]::after {
content: " ↗";
}
These snippets are intentionally small and focused. They are not meant to replace full plugins, but to cover common needs without adding extra dependencies or overhead.
Before using any snippet on a live site, test it on a staging or local setup first. Even simple code can behave differently depending on your theme or other plugins.
Over time, building your own small library of snippets like these can save a lot of manual work and keep your WordPress setup lean and easier to maintain.
If you have your own snippets that solve repetitive problems, keep them documented. Future you will thank you.
Written by Levon, Founder of DinoMatic
Hey, I'm Levon - a web developer who loves helping gambling and Forex affiliates build fast, SEO-friendly websites that convert. I've created WP themes like Spinoko, Akurai, and FXT, designed for lean setups that don't compromise on performance or rankings. I write from hands-on experience - I test, tweak, and share what works.