Advertisement
fauzanjeg

Custom Reading Time Meta Function

May 23rd, 2021
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.12 KB | None | 0 0
  1. /** Custom Reading Time Meta Function */
  2. function custom_reading_time_meta() {
  3.  
  4.     if ( \JNews\Single\SinglePost::getInstance()->show_reading_time_meta() ) {
  5.  
  6.         $output = '';
  7.  
  8.         if ( vp_metabox( 'jnews_single_post.override_template' ) ) {
  9.             $wpm = (int) vp_metabox( 'jnews_single_post.override.0.post_reading_time_wpm' );
  10.         } else {
  11.             $wpm = (int) get_theme_mod( 'jnews_single_reading_time_wpm', 300 );
  12.         }
  13.  
  14.         $content = get_post_field( 'post_content', get_the_ID() );
  15.  
  16.         if ( $content && $wpm ) {
  17.             $content    = strip_shortcodes( $content );
  18.             $word_count = str_word_count( $content );
  19.             $word_count = ceil( $word_count / $wpm );
  20.             $reading_time = jnews_return_translation( 'Reading Time: ', 'jnews', 'reading_time' ) . sprintf( _n( jnews_return_translation( '%d min read', 'jnews', 'min_read_s' ), jnews_return_translation( '%d mins read', 'jnews', 'min_read_p', 'jnews' ), $word_count ), $word_count );
  21.  
  22.             if ( $word_count ) {
  23.                 $output =
  24.                     "<div class=\"jeg_meta_reading_time\">
  25.                     <span>
  26.                         " . $reading_time . "
  27.                     </span>
  28.                 </div>";
  29.             }
  30.         }
  31.  
  32.         return jnews_sanitize_by_pass( $output );
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement