Skip to content

Instantly share code, notes, and snippets.

@hugodias
Created July 14, 2018 14:38
Show Gist options
  • Save hugodias/6622556ace1443d3927bde8282b50e36 to your computer and use it in GitHub Desktop.
Save hugodias/6622556ace1443d3927bde8282b50e36 to your computer and use it in GitHub Desktop.
Wordpress Reading time function with I18n Support
<?php
if ( ! function_exists( 'reading_time' ) ):
/**
* Display post reading time in minutes
*
* @param $post_content
*
* @return string
*/
function reading_time( $post_content ) {
$word_count = str_word_count( strip_tags( $post_content ) );
$readingtime = ceil( $word_count / 200 );
$est = sprintf( // WPCS: XSS OK.
esc_html( _nx(
'1 minute to read',
'%1$s minutes to read',
$readingtime,
'minutes to read',
'my_domain'
) ),
number_format_i18n( $readingtime )
);
return $est;
}
endif;
#: ../inc/extras:107
#, php-format
msgctxt "minutes to read"
msgid "1 minute to read"
msgid_plural "%1$s minutes to read"
msgstr[0] "1 minuto para ler"
msgstr[1] "%1$s minutos para ler"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment