Display Current Year in Wordpress

Display Current Year in Wordpress

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Displaying Dynamic Years in WordPress Content

In WordPress, the standard approach to displaying the current year relies on server-side PHP execution within template files. A common pattern found in footer templates looks like this:

<?php echo date('Y'); ?>

This works because the theme's footer.php file is processed by PHP before being sent to the browser, allowing you to access the core date() function directly. However, this method fails when placed inside post content or page content. WordPress intentionally strips out raw PHP tags from the main content area of posts and pages to prevent code injection and security vulnerabilities. When you paste that snippet into a standard editor, it renders as plain text rather than executing logic.

For scenarios like managing a car dealer website where year model references (e.g., "2013 Volkswagen Polo") need to reflect the current year automatically, relying on manual updates is unsustainable. The solution requires shifting from server-side execution to client-side manipulation or utilizing WordPress hooks that specifically target content rendering.

Leveraging JavaScript for Client-Side Updates

The most robust way to inject dynamic data into post content without touching the PHP logic of individual posts is via JavaScript. You can hook into the DOMContentLoaded event to find all elements containing year models and update them dynamically when the page loads.

Create a custom script in your theme's functions.php file or enqueue a dedicated JS file:

document.addEventListener('DOMContentLoaded', function() {
    const currentYear = new Date().getFullYear();
    
    // Select all h1, h2, p, or spans containing the year model pattern
    const yearElements = document.querySelectorAll('[data-update-year]');
    
    yearElements.forEach(element => {
        let content = element.innerHTML;
        
        // Simple regex replacement assuming the year is a standalone 4-digit number
        // Adjust this based on your specific HTML structure
        content = content.replace(/\b(19|20)\d{2}\b/, currentYear);
        
        element.innerHTML = content;
    });
});

To make this work, you must wrap the year model references in a specific data attribute, like <h1 data-update-year="2013">Volkswagen Polo</h1>, or rely on a strict regex if the context is controlled. This approach ensures the HTML structure remains intact while allowing the browser to render the correct year without requiring a database update.

Using WordPress Filters for Server-Side Injection

If you prefer keeping the logic on the server, you can use WordPress filters to modify content after it is loaded but before it is output. The the_content filter allows you to run PHP code against the post content in a safe environment.

In your theme's functions.php, add the following function:

function update_year_models($content) {
    $current_year = date('Y');
    
    // Check if this is a car dealer page or a specific post type if needed
    // Example: global $post; if ($post->post_type === 'car_inventory') { ... }
    
    // Replace year models in the content. 
    // Be careful with regex to avoid replacing years in URLs or other contexts.
    $content = preg_replace_callback('/\b(19|20)\d{2}\b/', function($matches) use ($current_year) {
        return $current_year;
    }, $content);
    
    return $content;
}
add_filter('the_content', 'update_year_models');

This method is generally safer than direct PHP in templates because it respects WordPress's sanitization layers. However, it has a significant limitation: it will update the year on every post and page where the content is displayed. If you have static pages describing historical models from 2013 that should never change, this filter will incorrectly rewrite them to the current year.

Trade-offs and Best Practices

When deciding between JavaScript and PHP filters, consider the scope of your update. Client-side JavaScript offers granular control; you can target specific post types or use data attributes to exclude static historical content. Server-side filters are easier to implement but lack context awareness unless coupled with conditional logic based on post metadata.

Additionally, be wary of simple string replacement or broad regex patterns in high-traffic sites. If a car model name contains the word "2013" as part of its history description (e.g., "The 2013 version was introduced"), blindly replacing all four-digit years could distort historical accuracy. A more sophisticated solution might involve maintaining a lookup table of known year models in a custom post type or meta field, updating those fields via a cron job on January 1st, and then displaying the stored value rather than calculating it dynamically.

For developers looking to manage complex content updates across large WordPress deployments without reinventing core functionality, exploring headless architectures or leveraging frameworks like Laravel for backend logic while serving static assets can provide a cleaner separation of concerns. Resources discussing modern WordPress development patterns, such as those available at LaravelCompany, often cover strategies for building scalable content management systems that handle dynamic data requirements more robustly than standard theme files alone.

Our network