Hi all,
I am working on a function in WP that searches the post content for any custom tags <%custom-tag%>
and if it finds tries to replace the tag with a file with the same name. Everything works fine but when I use apply_filters
to re-apply the content formatting WP also adds some closing tags, mostly </p>
in some of the included HTML which results in bad formatting HTML.
Any ideas on how to fix this? I tried applying the filters before including the content but it makes it even worse.
See function below:
//GET CUSTOM CONTENT WITH INSERTED TAGS
function extract_custom_value($string, $start, $end){
//make lower case
$string = strtolower($string);
//count how many tags
$count = substr_count($string, $start);
//create tags array
$custom_tags = array();
if ($count >= 1) {
//set the initial search position to 0
$pos_start = -1;
for ( $i = 0; $i < $count; $i++ ) {
//find custom tags positions
$pos_start = strpos($string, $start, $pos_start + 1);
$pos_end = strpos($string, $end, ($pos_start + strlen($start)));
//set start and end positions of custom tags
$pos1 = $pos_start + strlen($start);
$pos2 = $pos_end - $pos1;
//add to array
$custom_tags[$i] = substr($string, $pos1, $pos2);
}
return $custom_tags;
} else {
return false;
}
}
function get_custom_content(){
//get the content from wordpress
$content = get_the_content();
//find any custom tags
$custom_tags = extract_custom_value($content, '<%', '%>');
//if there is custom tags
if ( $custom_tags ) {
foreach ( $custom_tags as $tag ) {
//make file name from tag
$file = TEMPLATEPATH . '/' . $tag . '.php';
//check if it exists a file with the tag name
if ( is_file($file) ) {
//include the content of the file
ob_start();
include $file;
$file_content = ob_get_contents();
ob_end_clean();
} else {
$file_content = false;
}
//replace the tag with the file contents
$content = str_replace('<%' . $tag . '%>', $file_content, $content );
}
}
//re-apply WP formating to the content
$content = apply_filters('the_content', $content);
//clean up
$content = str_replace(']]>', ']]>', $content);
//show it
print $content;
}