Forked from avillegasn/estimar-tiempo-lectura-entrada-wordpress.php
Created
February 14, 2019 16:21
-
-
Save juananruiz/705e5e6490fca99871f9aa2c41bf0d71 to your computer and use it in GitHub Desktop.
Código para calcular la estimación de tiempo necesario para leer una entrada en WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Estima el tiempo necesario para leer una entrada en WordPress | |
* | |
* @return string | |
*/ | |
function wpr_estima_tiempo_lectura() { | |
$entrada = get_post(); | |
$ppm = 250; // podemos escoger el PPM que queramos (200-300, por ejemplo) | |
$palabras = str_word_count( strip_tags( $entrada->post_content ) ); | |
$minutos = floor( $palabras / $ppm ); | |
$segundos = floor( ( $palabras % $ppm ) / ( $ppm / 60 ) ); | |
if ( 1 <= $minutos ) { | |
$tiempo_estimado = $minutos . ' minuto' . ( 1 === $minutos ? '' : 's'); | |
if ( $segundos > 0 ) { | |
$tiempo_estimado .= ', ' . $segundos . ' segundo' . ( 1 === $segundos ? '' : 's' ); | |
} | |
} else { | |
$tiempo_estimado = $segundos . ' segundo' . ( 1 === $segundos ? '' : 's' ); | |
} | |
return $tiempo_estimado; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment