Site logo

Archived topic

How to add current year into a blog title?

9 replies · Started by Wendy on November 3, 2021

Viewing posts 1–10 of 10

Hello, I have a couple of articles that I insert the current date manually.

It's an annual admin task that I'm sure could be avoided through simple code??? But what??

Here is an example:

https://www.glow-skincare.com/argan-oil-vs-jojoba-oil/

Thank you :-)

Hi there,

so are you updating the date only on specific posts ?
Problem with doing it with code, is knowing which posts that change should be applied to and when....

let me know

Yes only on a handful of posts, so I would know their page ID?
25979
3585
21978
23857
24910
25486
25672
4751

You could use a Shortcode to display the Year in the title - this article explains one method:

https://technumero.com/auto-add-dynamic-year-in-wordpress-post-titles/

Note - that it includes the filter for do_shortcode in the_title, as well as some filters specifically for Yoast - THIS is important to note if you're using any SEO plugins or Feeds those too would require filtering.

Unfortunately something that seems very simple has several pitfalls....

Ooh yes, that's perfect now thankyou David. (also for the heads up on SEO plugins or Feeds..)

You're welcome!

Hello David, hope you are well!

I switched from Yoast to SEOPress on the weekend, and now in MonsterInsights I am seeing strange stuff:

instead of :Bach Flower Remedies List - 2022 Guide

they appear as Bach Flower Remedies List - [year] Guide

The current snippets refer to Yoast: - do I simply change Yoast to SEOPress?

Thank you so much. Current Snippet code pasted below.

//* Shortcode to display the current year in WordPress
//* shortcode: [year]
add_shortcode( 'year' , 'current_year' );
function current_year() {
$year = date("Y");
return "$year";
}

add_filter( 'the_title', 'do_shortcode' );//* Activate shortcode function in Post Title
add_filter( 'wpseo_title', 'do_shortcode' );//* Activate shortcode function in Yoast Title
add_filter( 'wpseo_metadesc', 'do_shortcode' );//* Activate shortcode function in Yoast Meta Description

So this:

//* Shortcode to display the current year in WordPress
//* shortcode: [year]
add_shortcode( 'year' , 'current_year' );
    function current_year() {
    $year = date("Y");
    return "$year";
}

//* Activate shortcode function in Post Title
add_filter( 'the_title', 'do_shortcode' );

Is the only code that will work anywhere regardless of what SEO plugin you're using.

The other two add_filters are 100% yoast specfic ie. wpseo_title and wpseo_metadesc.
You would need to ask SEO Press if they have their own filters for their SEO title and SEO descriptions.
You can share this topic with them so they can see what you need them for.

Thank you David!

I have now switched back to Yoast...it seems better geared up.

You're welcome

This archived topic is closed to new replies.