0

I wanted my search to exclude html tags so that in the search results page it wont show html tags and class. I'm a beginner in wordpress development so i find it hard to understand some functions. Here is my search.php:

<?php

/*
Template Name: Search Page
*/

get_header() ?>

<main>
    <div class="page-title above-content">
        <?php //$allsearch = new WP_Query("s=$s&showposts=0");
        ?>
        <h1>
            Search Results
        </h1>
    </div>
    <div class="content flex-col">
        <?php

        if (have_posts()) {
            while (have_posts()) {
                the_post();
                if ($s == '' or $s == ' ') {
                    echo '<p><div class="snackbar">No Results found! Please try again.</div></p>';
                    break;
                }
                ?>
                <!-- // Display the results -->
                <h3 class="search-post-title"><a href="<?php echo get_permalink(); ?>" target="_blank">
                        <?php
                                $title = get_the_title();
                                $keys = explode(" ", $s);
                                $title = preg_replace('/(' . implode('|', $keys) . ')/iu', '<strong class="search-excerpt">\0</strong>', $title);
                                echo $title;
                                ?>
                    </a>
                </h3>

                <?php
        
                $searchTxt = explode(" ", $s);

                $content = get_the_content();
                $content = explode("</p>", $content);
                for ($i = 0; $i < count($content); $i++) {
                    for ($j = 0; $j < count($searchTxt); $j++) {
                        if (str_contains($content[$i], $searchTxt[$j])) {
                            echo '<p>';
                            $content[$i] = preg_replace('/(' . implode('|', $searchTxt) . ')/iu', '<strong class="search-excerpt">\0</strong>', $content[$i]);
                            echo $content[$i] . '... <a href="' . get_permalink() . '" target="_blank">Continue Reading.</a></p>';
                            break;
                        }
                    }
                }

                ?>

                <!-- // Pagination -->

                <hr />
                <?php
            }
        } else {
            ?>
            <p>
                <?php //_e('Sorry, no posts matched your criteria.'); 
                    echo '<div class="snackbar">No Results found! Please try again.</div>';
                    ?>
            </p>
            <?php
        }

        ?>

        <div class="pagination">
            <?php
            
            wp_pagenavi(array('query'));

            wp_reset_postdata();
            ?>
        </div>
    </div>


</main>
<?php get_footer() ?>

This is the result for searching a '<' symbol.

enter image description here

1 Answer 1

0

There's a php function called strip_tags, see https://www.php.net/manual/en/function.strip-tags.php

You can see it implemented for your use case here: Strip HTML Tags From Search Results

Hope that helps.

3
  • Hi, thanks for answering my question. I have already looked on to that topic and tried applying 'strip_tags' on my $content variable. It only strip out all of the tags in the result page, but when i try searching for words or symbols included in an html tag it still show results without the tags. What i want is search function will only look into the content in between the html tags, not the html tags itself.
    – adriandneb
    May 10 at 8:44
  • OK got you. Then your search.php template is irrelevant. Is this Wordpress core search? Have you tried: wordpress.stackexchange.com/questions/56281/… I would then exclude '<' and/or '>'
    – user232080
    May 10 at 11:18
  • I checked on it, and it will definitely be useful on being specific on symbols and words. But I generally want to clear out whole html tags especially with class or ID. Ex. <p class="something">, I want the whole tag including the class to be excluded on search, because there might be a class tag that might match with the content. Thank you for the help, appreciate it.
    – adriandneb
    May 11 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.