Add time ago in wordpress post or comment without plugin, simple php function to add time ago in wordpress easily, two ways to add time ago, manual way and automatic way.
How To Add Time Ago In WordPress
Very easy, using human_time_diff() function we will add time ago in wordpress post or comment.
Add Time Ago In Posts
We have 2 ways, manual way and automatic way, this is manual way, copy function code and paste it in your functions.php file:
function WPTime_get_post_time_ago(){ // By Qassim Hassan global $post; return human_time_diff( get_post_time('U', false, $post->ID), current_time('timestamp') )." Ago"; }
Now to display time ago open post loop file and echo function, for example in twenty fifteen theme post loop file is content.php, paste this code inside post loop:
echo WPTime_get_post_time_ago();
Now will be display time ago.
Automatic Way: Copy code and paste it in your functions.php file:
function WPTime_get_post_time_ago(){ // By Qassim Hassan global $post; return human_time_diff( get_post_time('U', false, $post->ID), current_time('timestamp') )." Ago"; } if( !is_admin() ){ // By Qassim Hassan add_filter('get_the_date', 'WPTime_get_post_time_ago'); add_filter('get_the_time', 'WPTime_get_post_time_ago'); add_filter('the_time', 'WPTime_get_post_time_ago'); }
Add Time Ago In Comments
We have 2 ways, manual way and automatic way, this is automatic way, copy code and paste it in your functions.php file:
function WPTime_get_comment_time_ago(){ // By Qassim Hassan return human_time_diff( get_comment_time('U'), current_time('timestamp') )." Ago"; } if( !is_admin() ){ // By Qassim Hassan add_filter('get_comment_date', 'WPTime_get_comment_time_ago'); }
Manual Way: Echo function inside comment callback:
echo WPTime_get_comment_time_ago();
Done.
You can use Time Ago plugin.