0

How to change published date (posted on) to last modified date on your WordPress site

If you are starting your own WordPress site or blog, I’m sure this question has come up at some point. Keeping your content updated is one of most important things for your website users and for search engines. I’m here to show you how can you do exactly that for your Hiero WordPress theme, but it should also work with some modifications on other themes.

I apologize in advance to my crypto readers since this post will have nothing to do with crypto, but I will drift away from crypto themes from time to time. Also, since most of building blocks for this blog / website are free and open source, I feel obligated to publish my changes so others may use them and benefit from them.

Problem

As stated before, users and search engines appreciate websites and authors who keep their content relevant and updated. If you publish a post in WordPress today on February 22nd and updated it with more relevant information in three months, WordPress will still keep “Posted on February 22nd” text and related meta tags as post date.

Solution

You need to add date and time of last content modification to your blog post. But this is where the trouble starts. How do you want to publish that date?

Posted on February 22
Last updated on May 23

Posted on February 22, last updated on May 23

Last updated on May 23

Posted on on May 23

My choice was to replace the original text and date to reflect that post was updated after publication. Instructions are focused on solving this problem for Hiero WordPress theme.

Create child theme

First, you need to create child theme. Instruction on how to do it can be found in the WPbeginner blog post, but the fastest and easiest way is with Child Theme Configurator plugin. It will take just few minutes to create child theme and you can remove or disable plugin when you’re done.

Modify functions.php file

We need to find which part of code to modify. To find it, open your website, right click on the date of your post and click on “Inspect”. This will open developer console and highlight parts of source code you are interested in.

Here you will learn that for Hiero theme, you should focus on this class:

class=”entry-date published updated”

Now you need to search your theme files to find this class. Personally, I’m using grepWin from Stefan tools on Windows to search inside all files from Hiero theme. The search tool will point you to “template-tags.php” file and line 169 which is part of function athemes_posted_on(). We have some code to work with now!

function athemes_posted_on() {
	$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
	$time_string = sprintf( $time_string,
		esc_attr( get_the_date( 'c' ) ),
		esc_html( get_the_date() ),
		esc_attr( get_the_modified_date( 'c' ) ),
		esc_html( get_the_modified_date() )
	);

	printf( __( '<span class="posted-on">Posted on %1$s</span><span class="byline"> by %2$s</span>', 'hiero' ),
		sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
			esc_url( get_permalink() ),
			esc_attr( get_the_time() ),
			$time_string
		),
		sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
			esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
			esc_attr( sprintf( __( 'View all posts by %s', 'hiero' ), get_the_author() ) ),
			esc_html( get_the_author() )
		)
	);
}

Next step is to change the code to suit our needs. Following was modified:

  • Added IF statement that shows “Last updated on” text if the post was modified more than 7 days (604800 seconds) after first publication. I’m leaving this time for first post editing when you usually fix spelling errors etc.
  • Link which was on date pointing back to post URL is removed in “printf” part, since it didn’t make sense to me to have link in post pointing to that same post.

New code:

function athemes_posted_on() {
	$date = get_the_date( 'U' );
	$updated = get_the_modified_date( 'U' );

	if( $updated > ( $date + 604800 ) )
		$time_string = 'Last updated on <time class="entry-date published updated" datetime="%3$s">%4$s</time>';
	else {
		$time_string = 'Posted on <time class="entry-date published updated" datetime="%1$s">%2$s</time>';
	}
	
	$time_string = sprintf( $time_string,
		esc_attr( get_the_date( 'c' ) ),
		esc_html( get_the_date() ),
		esc_attr( get_the_modified_date( 'c' ) ),
		esc_html( get_the_modified_date() )
	);

	printf( __( '<span class="posted-on">%1$s</span><span class="byline"> by %2$s</span>', 'hiero' ),
		sprintf( '%3$s',
			esc_url( get_permalink() ),
			esc_attr( get_the_time() ),
			$time_string
		),
		sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
			esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
			esc_attr( sprintf( __( 'View all posts by %s', 'hiero' ), get_the_author() ) ),
			esc_html( get_the_author() )
		)
	);
}

Now copy this code and paste it into the child theme functions.php file and you are done. If you don’t want to use child theme, you can also replace the original code in template-tags.php with this code, but if somewhere in the future you update your theme, you will loose changes.

Thanks to Ariel Lim for parts of IF statement code, I used it since I’m lazy and it was easier. 🙂 You can check his blog post if you aren’t using Hiero theme, maybe it will be helpful.

Libertus Avatar
Libertus

Crypto enthusiast since February 2018 and apparently popular scriptwriter on Tradingview. :) Working in a senior management position in a company that has absolutely nothing to do with crypto or capital markets. This is my personal blog that aims to help people understand crypto and make it easier for them to make their own conclusions. Nothing I write here is an investment advice or advice to buy. Always do extensive research prior to buying or investing your hard-earned money in anything, especially cryptocurrencies. Cryptocurrency space is more than 90% scam, so losing money is very easy. Since I am not a native English speaker, forgive me for any spelling mistakes.

Leave a Reply

Your email address will not be published.