tags:

views:

16

answers:

1

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(']]>', ']]&gt;', $content);
    //show it
    print $content;
}
+1  A: 

Thanks to Richard M pointing me in the right direction to use WP short-code API, I have fixed the problem and have now a much leaner script. Here it is in case someone want to know how:

function insert_file($atts){

    extract(shortcode_atts(array(
    'file' => false
    ), $atts));

    if ( $file == false ){
        $file_content = false;
    }else{
        $file = TEMPLATEPATH . '/' . $file . '.php';

        if ( is_file($file) ) {
            ob_start();
            include $file;
            $file_content = ob_get_contents();
            ob_end_clean();

        } else {
            $file_content = false;
        }
    }

    return $file_content;

}
add_shortcode('insert', 'insert_file');
Pedro Reis