What is the best method for intercepting the_content of a post (for a single post page) and dividing the_content into sections of 500 characters (or any other number of characters), and then outputting each 500-character section wrapped in its own div container?
I understand that get_the_content() will return the post content as a string that can be stored in a variable. But, being new to PHP, I’m not sure how to go about segmenting the content based on character count. We could get the character count using:
<?php
$content = get_the_content();
echo strlen($content);
?>
But that is as far as my knowledge goes.
Can anyone help me out with this challenge?
UPDATE:
With some help from Lukas Kolletzki on stackoverflow I have gotten this far:
<?php
$content = get_the_content();
$chunks = str_split($content, 500);
//Printing each chunk in a div
foreach($chunks as $chunk_content) {
echo "<div>";
echo $chunk_content;
echo "</div>";
}
?>
This successfully splits the post content into 500-word sections and wraps each of them into separate containers.
Going even further, Lukas suggested using the wordwrap() and explode() functions to ensure that words weren’t cut in half:
<?php
$content = get_the_content();
$strings = wordwrap($content, 500, "<br />"); //Put a {BREAK} every 500 characters
$chunks = explode("<br />", $strings); //Put each segment separated by {BREAK} into an array field
//Printing each chunk in a div
foreach($chunks as $chunk_content) {
echo "<div class="post-content">";
echo $chunk_content;
echo "</div>";
}
?>
This works quite nicely, but now I am realizing that the normal wordpress
tags are not being output. Is this because get_the_content() outputs only text, without the html?
How can I get the paragraphs back into the post content and still maintain the above functionality?
Thanks again, and please do not mark this as duplicate. It’s a much different question than the excerpt_length filter question… or at least I think so.
get_the_content()
return what’s in the editor, butwpautop
filters and such are attached tothe_content
(which you don’t need inside your split function – just apply it later manually withat the end.
You should as well use
strip_shortcodes( get_the_content() );
before splitting it up:You’ll need to loop through it as long as you got content and
array_push()
to your$output = array();
.so I really appreciate all the feedback and the answer that @kaiser and @t31os contributed. I am shaky with PHP so it was hard to put together a working block of code with kasier & t31os’s answer. That’s my fault for not being a very talented coder 🙁
But, I wanted to post the solution that I came up with by doing my own research on PHP.net and asking a similar question on StackOverflow.
Here is the code I’m using to accomplish what I originally set out to do:
NOTES:
The purpose of this segmenting of the content is to create separate frames or “pages” that will be used by the BookBlock page-flip plugin (check out this codrops tutorial for more info). Each 500-character section will be treated as a “page”, and the navigation controls on the page allow the user to flip through the pages with a nice CSS3 3D transform transition.
I used
<span>
containers to wrap the output because this method lends itself to graceful degradation. If the user’s device does not support Javascript or some essential CSS3 properties, then Modernizer will detect that and my CSS will adapt, showing a normal page of content, not broken into separate frames, and in this case the<span>
won’t disrupt the normal paragraph structure of the content.I used output buffering in order to compile all the 500-char sections into one variable to be passed through the
nl2br();
filter for final output.At first I tried using the
wpautop();
filter to ensure that paragraphs were rendered properly in the output, but soon remembered that<span>
and<p>
tags cannot overlap 😛 And if I applied thewpautop();
filter to each 500-char section individually, it would output<p>
tags in the middle of sentences sometimes. The solution was to use thenl2br();
filter in order to add<br>
tags at every line break in the content, which, because they don’t require a closing tag, will maintain the paragraph structure without disrupting the 500-char containers.QUESTION: This code works, and it will be used on a website that will not have heavy traffic (probably less that 50 visits per day). But, I am still concerned (because I really am not very knowledgeable about PHP) that this code may be sluggish, and not be very elegant.
Assuming I knew exactly how accomplish what kaiser and t31os are recommending in their answer, would their solution be better for computation speed (i.e. fewer server requests, db queries, less code, etc.)?
Or is my solution just as good?
Per a request from user2655393 I have written a version of his/her code that does not use output buffering, but uses string concatenation instead.