Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 906777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:26:42+00:00 2026-05-15T16:26:42+00:00

I am working on a function in WP that searches the post content for

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:26:43+00:00Added an answer on May 15, 2026 at 4:26 pm

    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');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a search function that finds the song where the search matches
I have a function that searches for a string inside a text file. I
I'm working on a function that is too complicated (processor) so I embedded part
I am working on a function that accepts a JSON object as a parameter.
I'm working on a function that handles events from a number of buttons and
I am working on a function that will essentially see which of two ints
I'm working with a PHP function that takes a string and converts all of
I'm working in PHP and I want to create a function that, given a
I search for the function that run programm by path, and working of main
Working with an undisclosed API, I found a function that can set the number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.