Advertisement
arie_cristianD

add reading time meta on jnews module 1

Jun 22nd, 2025
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.44 KB | None | 0 0
  1. add_filter( 'jnews_module_post_meta_1', 'override_jnews_meta_1', 99, 3 );
  2.  
  3.  
  4. function override_jnews_meta_1( $output, $post, $instance ) {
  5.     if ( 'JNews\Module\Block\Block_1_View' === get_class( $instance ) ) {
  6.         $output = '';
  7.  
  8.         if ( get_theme_mod( 'jnews_show_block_meta', true ) ) {
  9.             $comment       = jnews_get_comments_number( $post->ID );
  10.             $view_count    = jnews_meta_views( $post->ID );
  11.             $author        = isset( $post->post_author ) ? $post->post_author : 0;
  12.             $author_url    = get_author_posts_url( $author );
  13.             $author_name   = get_the_author_meta( 'display_name', $author );
  14.             $author_avatar = false ?
  15.                 '<div class="jeg_author_avatar">
  16.                    ' . get_avatar( get_the_author_meta( 'ID', $post->post_author ), 80, null, get_the_author_meta( 'display_name', $post->post_author ) ) . '
  17.                </div>' : '';
  18.  
  19.             $trending = ( jnews_get_metabox_value( 'jnews_single_post.trending_post', null, $post->ID ) ) ? '<div class="jeg_meta_trending"><a href="' . get_the_permalink( $post ) . '" aria-label="' . esc_html__( 'View this Trending Post', 'jnews' ) . '"><i class="fa fa-bolt"></i></a></div>' : '';
  20.  
  21.             if ( jnews_is_review( $post->ID ) ) {
  22.                 $rating = jnews_generate_rating( $post->ID, 'jeg_landing_review' );
  23.  
  24.                 $output .= '<div class="jeg_post_meta">';
  25.                 $output .= $trending;
  26.                 $output .= get_theme_mod( 'jnews_show_block_meta_rating', true ) ? $rating : '';
  27.                 $output .= get_theme_mod( 'jnews_show_block_meta_author', true ) ? ( jnews_check_coauthor_plus() ? '<div class="jeg_meta_author coauthor">' . jnews_get_author_coauthor( $post->ID, false, 'by', 1 ) . '</div>' : '<div class="jeg_meta_author"><span class="by">' . jnews_return_translation( 'by', 'jnews', 'by' ) . "</span> <a href=\"{$author_url}\">{$author_name}</a></div>" ) : '';
  28.                 $output .= '</div>';
  29.             } else {
  30.                 $output .= '<div class="jeg_post_meta">';
  31.                 $output .= $trending;
  32.                 $output .= get_theme_mod( 'jnews_show_block_meta_author', true ) && ! empty( $author_name ) ? ( jnews_check_coauthor_plus() ? '<div class="jeg_meta_author coauthor">' . jnews_get_author_coauthor( $post->ID, $avatar, 'by', 1 ) . '</div>' : '<div class="jeg_meta_author">' . $author_avatar . '<span class="by">' . jnews_return_translation( 'by', 'jnews', 'by' ) . "</span> <a href=\"{$author_url}\">{$author_name}</a></div>" ) : '';
  33.                 $output .= custom_reading_time( $post->ID );
  34.                 $output .= get_theme_mod( 'jnews_show_block_meta_date', true ) ? '<div class="jeg_meta_date"><a href="' . get_the_permalink( $post ) . '"><i class="fa fa-clock-o"></i> ' . $instance->format_date( $post ) . '</a></div>' : '';
  35.                 $output .= get_theme_mod( 'jnews_show_block_meta_comment', true ) ? '<div class="jeg_meta_comment"><a href="' . jnews_get_respond_link( $post->ID ) . "\" ><i class=\"fa fa-comment-o\"></i> {$comment} </a></div>" : '';
  36.                 $output .= get_theme_mod( 'jnews_show_block_meta_views', false ) ? '<div class="jeg_meta_views"><a href="' . get_the_permalink( $post->ID ) . "\" ><i class=\"fa fa-eye\"></i> {$view_count} </a></div>" : '';
  37.                 $output .= '</div>';
  38.             }
  39.         }
  40.     }
  41.  
  42.     return $output;
  43. }
  44.  
  45.  
  46. function custom_reading_time( $post_id ) {
  47.                 $output = '';
  48.  
  49.     if ( jnews_get_metabox_value( 'jnews_single_post.override_template' ) ) {
  50.         $wpm = (int) jnews_get_metabox_value( 'jnews_single_post.override.0.post_reading_time_wpm' );
  51.     } else {
  52.         $wpm = (int) get_theme_mod( 'jnews_single_reading_time_wpm', 300 );
  53.     }
  54.  
  55.             $content = get_post_field( 'post_content', $post_id );
  56.  
  57.     if ( $content && $wpm ) {
  58.         $content      = strip_shortcodes( $content );
  59.         $word_count   = ( 'str_word_count' === get_theme_mod( 'jnews_calculate_word_method', 'str_word_count' ) ) ? str_word_count( $content ) : substr_count( $content, ' ' ) + 5; /* see p3HUlGhX */
  60.         $word_count   = ceil( $word_count / $wpm );
  61.         $reading_time = jnews_return_translation( 'Reading Time: ', 'jnews', 'reading_time' );
  62.         if ( defined( 'JNEWS_FRONT_TRANSLATION' ) ) {
  63.             $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 );
  64.         } else {
  65.             $reading_time .= sprintf( _n( '%d min read', '%d mins read', $word_count, 'jnews' ), $word_count );
  66.         }
  67.  
  68.         if ( $word_count ) {
  69.             $output =
  70.                 '<div class="jeg_meta_reading_time">
  71.                         <span>
  72.                             ' . $reading_time . '
  73.                         </span>
  74.                     </div>';
  75.         }
  76.     }
  77.  
  78.     return $output;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement