Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Last active October 22, 2021 22:08
Show Gist options
  • Save ihorduchenko/37c573f69a4bf2a2c73a91546e6974d3 to your computer and use it in GitHub Desktop.
Save ihorduchenko/37c573f69a4bf2a2c73a91546e6974d3 to your computer and use it in GitHub Desktop.
Hooking into WP Reading time plugin - Advanced custom fields (ACF) + Flexible content (rtwp_filter_wordcount filter)
function up_the_count( $count ) {
global $post;
$id = $post->ID;
$post_type = get_post_type($id);
switch ($post_type) {
case 'post':
if ( have_rows( 'content', $id ) ):
while ( have_rows( 'content', $id ) ) : the_row();
if ( get_row_layout() == 'paragraph_section' ):
$count += count( preg_split( '/\s+/', get_sub_field( 'paragraph' ) ) );
elseif ( get_row_layout() == 'wysiwyg_section' ):
$count += count( preg_split( '/\s+/', get_sub_field( 'editor' ) ) );
elseif ( get_row_layout() == 'blockquote_section' ):
$count += count( preg_split( '/\s+/', get_sub_field( 'blockquote_content' ) ) );
$count += count( preg_split( '/\s+/', get_sub_field( 'blockquote_footer' ) ) );
endif;
endwhile;
endif;
break;
case 'cases':
if ( have_rows( 'sections', $id ) ):
while ( have_rows( 'sections', $id ) ) : the_row();
if ( get_row_layout() == 'background_section' ):
$count += count( preg_split( '/\s+/', get_sub_field( 'background' )['title'] ) );
$count += count( preg_split( '/\s+/', get_sub_field( 'background' )['text'] ) );
$count += count( preg_split( '/\s+/', get_sub_field( 'background' )['legend_title'] ) );
$count += count( preg_split( '/\s+/', get_sub_field( 'background' )['legend'] ) );
elseif ( get_row_layout() == 'client_section' ):
$count += count( preg_split( '/\s+/', get_sub_field( 'client' )['title'] ) );
$count += count( preg_split( '/\s+/', get_sub_field( 'client' )['text'] ) );
elseif ( get_row_layout() == 'challenge_section' ):
$count += count( preg_split( '/\s+/', get_sub_field( 'challenge' )['title'] ) );
$count += count( preg_split( '/\s+/', get_sub_field( 'challenge' )['text'] ) );
elseif ( get_row_layout() == 'result_section' ):
$count += count( preg_split( '/\s+/', get_sub_field( 'result' )['title'] ) );
$count += count( preg_split( '/\s+/', get_sub_field( 'result' )['text'] ) );
elseif ( get_row_layout() == 'blockquote_section' ):
$count += count( preg_split( '/\s+/', get_sub_field( 'blockquote' )['content'] ) );
elseif ( get_row_layout() == 'basic_section' ):
$count += count( preg_split( '/\s+/', get_sub_field( 'section' )['title'] ) );
$count += count( preg_split( '/\s+/', get_sub_field( 'section' )['text'] ) );
endif;
endwhile;
endif;
break;
default:
$count = $count;
break;
}
return $count;
}
add_filter( 'rtwp_filter_wordcount', 'up_the_count' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment