lol when i started testing i figured i was'nt so bright as i thought i was. Needed to following changes to do what it is supposed to do.
- Code: Select all
<?php
/*
Plugin Name: Short.ShortStory
Plugin URI: http://cutenews.ru
Description: Adds a {tag} to the templates to show n characters of the Short Story.
Version: 1.0
Author: FI-DD
Author URI: http://english.cutenews.ru/forum/profile.php?mode=viewprofile&u=2
*/
add_filter('news-entry', 'shortShortStory');
function shortShortStory($output) {
global $row, $output;
preg_match('/{sShortStory\|(\d+)}/i', $output, $temp);
$short_tmp = strip_tags($row['short']);
$long = strlen($row['short']);
$short = strlen($short_tmp);
$add = $long - $short;
$temp[1] = $temp[1] + $add;
$output = preg_replace('/{sShortStory\|(\d+)}/i', strlen($row['short']) > $temp[1] ? substr($row['short'], 0, $temp[1]).'...' : $row['short'], $output);
$output = run_filters('news-entry-content', $output);
return $output;
}
add_filter('template-variables-active', 'shortShortStoryTemplate');
add_filter('template-variables-full', 'shortShortStoryTemplate');
function shortShortStoryTemplate($template) {
$template['{sShortStory|n}'] = 'Shortens the Short Story to "My short story ..." (n = number of characters)';
return $template;
}
?>
This way it filters html from counting. But since you are nog showing the stripped version on your website you want to add to difference between the length of the stripped content with the non-stripped content. That way it wont break down in the middle of html tag.
---
Edit: Figured it does need more changes.. It wont break down in the middle of a html tag, but it can still break down in the content of an html tag. Which could cause tags not to be closed. I need to change it further lol

---
Edit: Found it
- Code: Select all
<?php
/*
Plugin Name: Short.ShortStory
Plugin URI: http://cutenews.ru
Description: Adds a {tag} to the templates to show n characters of the Short Story.
Version: 1.0
Author: FI-DD
Author URI: http://english.cutenews.ru/forum/profile.php?mode=viewprofile&u=2
*/
add_filter('news-entry', 'shortShortStory');
function strip_tags_content($text, $tags = '', $invert = FALSE) {
preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
$tags = array_unique($tags[1]);
if(is_array($tags) AND count($tags) > 0) {
if($invert == FALSE) {
return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text);
}
else {
return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text);
}
}
elseif($invert == FALSE) {
return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
}
return $text;
}
function shortShortStory($output) {
global $row, $output;
preg_match('/{sShortStory\|(\d+)}/i', $output, $temp);
$short_tmp = strip_tags_content($row['short']);
$long = strlen($row['short']);
$short = strlen($short_tmp);
$add = $long - $short;
$temp[1] = $temp[1] + $add;
$output = preg_replace('/{sShortStory\|(\d+)}/i', strlen($row['short']) > $temp[1] ? substr($row['short'], 0, $temp[1]).'...' : $row['short'], $output);
$output = run_filters('news-entry-content', $output);
return $output;
}
add_filter('template-variables-active', 'shortShortStoryTemplate');
add_filter('template-variables-full', 'shortShortStoryTemplate');
function shortShortStoryTemplate($template) {
$template['{sShortStory|n}'] = 'Shortens the Short Story to "My short story ..." (n = number of characters)';
return $template;
}
?>
------
Edit: Yet another edit needed to make it better. Not only HTML had to be taken care of. Also BB-code needed to be filtered for those who use quick_tags.\
- Code: Select all
<?php
/*
Plugin Name: Short.ShortStory
Plugin URI: http://cutenews.ru
Description: Adds a {tag} to the templates to show n characters of the Short Story.
Version: 1.0
Author: FI-DD
Author URI: http://english.cutenews.ru/forum/profile.php?mode=viewprofile&u=2
*/
add_filter('news-entry', 'shortShortStory');
function strip_tags_content($text) {
$htmlstrip = preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text); // Strips the HTML
$bbstrip = preg_replace('#\[(.*?)\](.*?)\[/(.*?)\]#si', '', $htmlstrip); // Strips BBCode, you need to strip it cause bbcode will turn into html and therefore - if not taken care of - could potentially screw your page up
return $bbstrip;
}
function shortShortStory($output) {
global $row, $output;
preg_match('/{sShortStory\|(\d+)}/i', $output, $temp);
$short_tmp = strip_tags_content($row['short']);
$long = strlen($row['short']);
$short = strlen($short_tmp);
$add = $long - $short;
$temp[1] = $temp[1] + $add;
$output = preg_replace('/{sShortStory\|(\d+)}/i', strlen($row['short']) > $temp[1] ? substr($row['short'], 0, $temp[1]).'...' : $row['short'], $output);
$output = run_filters('news-entry-content', $output);
return $output;
}
add_filter('template-variables-active', 'shortShortStoryTemplate');
add_filter('template-variables-full', 'shortShortStoryTemplate');
function shortShortStoryTemplate($template) {
$template['{sShortStory|n}'] = 'Shortens the Short Story to "My short story ..." (n = number of characters)';
return $template;
}
/* NOTE: Function is still incomplete. {nl} should somehow be accounted for. If you got multiple
paragraphs you use more space then when you don't. Somehow that should matter, but it does'nt yet.
To make it complete it should count the number of {nl} in $bbstrip and then multiply that amount
with a - yet to determine - agreeable amount of charactars(depends on the amount of charactars that fit in one line).
That amount needs to be deducted from $temp[1].
LOL the only person who probably understands what i'm saying is FI-DD, so cheers mate :P I can't figure it out
she is al yours.
*/
?>
---
Beginning to look like this function won't ever work flawless. If your tags are stripped outside the text that is truncated, they will still add to your $temp[1] which will make the truncated text longer then it should be. It should only count the amount of characters stripped in the first $temp[1] characters.