Last active
September 5, 2024 01:43
-
-
Save danielbachhuber/6691084 to your computer and use it in GitHub Desktop.
Revisions
-
danielbachhuber revised this gist
Oct 13, 2013 . 1 changed file with 13 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -12,6 +12,12 @@ if ( 900 > str_word_count( $content ) ) return; if ( false !== stripos( $content, '<!--nextpage-->' ) || false !== stripos( $content, '<!--nopage-->' ) ) return; $content = wpautop( $content ); $content_array = str_split( $content ); $word_array = str_word_count( $content, 2 ); $word_count = 0; @@ -28,11 +34,16 @@ } // Found a '<p>' if ( '<' == $content_array[$i-1] ) { $k = $i-2; } // Found a '</p>' else if ( '<' == $content_array[$i-2] ) { $k = $i+3; } else { $word_count++; continue; } $k = $k + ( $next_page_count * 15 ); $next_page_count++; -
danielbachhuber revised this gist
Sep 24, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,14 +9,14 @@ $content = $query->posts[0]->post_content; if ( 900 > str_word_count( $content ) ) return; $content_array = str_split( $content ); $word_array = str_word_count( $content, 2 ); $word_count = 0; $next_page_count = 0; while ( count( $word_array ) > 900 ) { $word_array = array_slice( $word_array, 500 + $word_count, null, true ); $word_count = 0; -
danielbachhuber created this gist
Sep 24, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ <?php /** * Auto-paginate after 500 words */ add_action( 'loop_start', function( $query ) { if ( ! is_single() || 'post' != get_post_type() || ! $query->is_main_query() ) return; $content = $query->posts[0]->post_content; if ( 800 > str_word_count( $content ) ) return; $content_array = str_split( $content ); $word_array = str_word_count( $content, 2 ); $word_count = 0; $next_page_count = 0; while ( count( $word_array ) > 700 ) { $word_array = array_slice( $word_array, 500 + $word_count, null, true ); $word_count = 0; foreach( $word_array as $i => $word ) { if ( 'p' != $word ) { $word_count++; continue; } // Found a '<p>' if ( '<' == $content_array[$i-1] ) $k = $i-2; // Found a '</p>' else $k = $i+3; $k = $k + ( $next_page_count * 15 ); $next_page_count++; $content = substr( $content, 0, $k ) . '<!--nextpage-->' . substr( $content, $k ); break; } } $query->posts[0]->post_content = $content; });