Recently I working on a little pet project of mind in which the content will not be created by me however will be generated by my logged in users. The problem with letting other people into the website and have control over it is that as a coder I need to add in fail safes, and make ever step idiot proof. In this case I need to make sure that the user doesn’t create an epic saga of text and post it on the main page. Like most of my projects, it uses the WordPress engine to allow the users to enter in their data. WordPress has a two built in methods to decrease the output of text to a more manageable size.
1. The Exerpt field under the main post area. This is a manual area in which the user/writer adds a smaller version of the post above. I use this method for this website but for the other I can’t guaranty that every user/writer will as well meaning the excerpt will be blank. So that option is out.
2. <–more–>, when added to a post it cuts the content and hide it for the general listing of posts however it when ti post is shown on it’s own (single.php), the entire content is seen. BUT, once again I can’t guaranty that every user/writer will this. Meaning the intended excerpt will not be blank like the above method but have the potential to scroll forever. So that option is also out.
So… WordPress. I Love U, but you didn’t think of everything. My fix:
Generic
<?php
$tss_exc_size = 110;
$tss_content = '<strong>Lorem ipsum</strong> dolor sit amet, consectetur adipiscing elit.
Morbi sapien sem, elementum id viverra ac, posuere ac magna. Fusce ut nisl nisl. Nullam non tellus
imperdiet dolor pulvinar pretium id id orci. Duis egestas laoreet enim vel malesuada. Pellentesque
habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec ut justo
nibh venenatis consequat non a urna. Vivamus quis mi leo. <em>Nullam velit mi, posuere id
mollis i, luctus pretium felis. Iun hac habitasse platea dictumst</em>. Quisque suscipit suscipit
mi et lobortis. Integer a sem sed felis interdum ultrices vitae at velit. Donec purus metus, luctus eu
dapibus ihn, bibendum eu mauris. Aliquam scelerisque sem pin dolor egestas at pellentesque est
rhoncus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vulputate <a href="#">ipsum</a>
ac massa vulputate consequat. Fusce quis consequat nunc. Class aptent taciti sociosqu ad litora
torquent per conubia nostra, per inceptos himenaeos. Phasellus iaculis justo eget mi cursus porta. ';
$tss_thiscontent = strip_tags( $tss_content );
$tss_content_length = strlen($tss_thiscontent);
if($tss_content_length <= $tss_exc_size) {
$tss_thisexcerpt = '<div class="tss_excerpt">' . $tss_thiscontent . '</div>';
}else{
$tss_new_length = $tss_content_length - $tss_exc_size;
$tss_thisexcerpt = '<div class="tss_excerpt">' . substr($tss_thiscontent, 0, -$tss_new_length);
$tss_thisexcerpt .= '...</div>';
};
echo $tss_thisexcerpt;
?>
PHP Native Tags:
$ : Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
= : Set the value of a variable
.= : Concatenates on to the existing string
PHP Native Functions used:
strip_tags() : This function tries to return a string with all HTML and PHP tags stripped from a given string.
strlen() : Returns the length of the given string.
substr() : Returns the portion of string specified by the start and length parameters.
WordPress specific:
<?php
$tss_exc_size = 110;
$tss_thiscontent = strip_tags( get_the_content() );
$tss_content_length = strlen($tss_thiscontent);
if($tss_content_length <= $tss_exc_size) {
$tss_thisexcerpt = '<div class="tss_excerpt">' . $tss_thiscontent . '</div>';
}else{
$tss_new_length = $tss_content_length - $tss_exc_size;
$tss_thisexcerpt = '<div class="tss_excerpt">' . substr($tss_thiscontent, 0, -$tss_new_length);
$tss_thisexcerpt .= '...</div>';
};
echo $tss_thisexcerpt;
?>
WordPress Function used:
get_the_content() : Retrieve the post content without echoing it.
I want to convert this to a function, and i want to limit the string from database. thanks
Ok so new post: Limit Characters in a PHP String: Revisited. As for the database part just feed the output of the function in to a insert/update SQL statement. If you need anything else just ask, but please don’t follow up a “I want” with another one, in text it comes off as demanding action and not asking for help.
Hrmm that was weird, my comment got eaten. Anyway I wanted to say that it’s nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer.
Nice experiments
I’d love to use this in next WP theme. Thanks a lot Max.
Regards