Skip to content

Instantly share code, notes, and snippets.

@premanshup
Created August 3, 2021 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save premanshup/ceb0fe7edcb558fc70eb90047f798b8d to your computer and use it in GitHub Desktop.
Save premanshup/ceb0fe7edcb558fc70eb90047f798b8d to your computer and use it in GitHub Desktop.
Astra - Disable "Reading time" on specific blog posts
add_action( 'init', 'astra_remove_read_time_filter', 11 );
function astra_remove_read_time_filter() {
$obj = Astra_Ext_Blog_Pro_Markup::get_instance();
remove_filter( 'astra_meta_case_read-time', array( $obj, 'reading_time_content' ), 10, 3 );
}
add_filter( 'astra_meta_case_read-time', 'astra_override_reading_time', 10, 3 );
function calculate_reading_time( $post_id ) {
$post_content = get_post_field( 'post_content', $post_id );
$stripped_content = strip_shortcodes( $post_content );
$strip_tags_content = wp_strip_all_tags( $stripped_content );
$word_count = count( preg_split( '/\s+/', $strip_tags_content ) );
$reading_time = ceil( $word_count / 220 );
return $reading_time;
}
function astra_override_reading_time( $content = '', $loop_count = '', $separator = '' ) {
if ( 1 !== get_the_ID() ) {
$read_time = (int) calculate_reading_time( get_the_ID() );
$singular_min_reading_text = apply_filters( 'astra_post_minute_of_reading_text', __( 'minute of reading', 'astra-addon' ) );
$plural_mins_reading_text = apply_filters( 'astra_post_minutes_of_reading_text', __( 'minutes of reading', 'astra-addon' ) );
$content .= ( 1 != $loop_count && '' != $content ) ? ' ' . $separator . ' ' : '';
/* translators: %1$s: $read_time the time to read the article, %2%s: $singular_min_reading_text the singular minute reading time text, %3%s: $plural_mins_reading_text the plural minutes reading time text */
$content .= '<span class="ast-reading-time">' . sprintf( _n( '%1$s %2$s', '%1$s %3$s', $read_time, 'astra-addon' ), $read_time, $singular_min_reading_text, $plural_mins_reading_text ) . '</span>'; // phpcs:ignore WordPress.WP.I18n.MismatchedPlaceholders
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment