WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I make a search, if there is an image title within a img tag, it's displayed in a very ugly fashion among the search results (the whole tag is displayed and the picture too). I found this script which is supposed to solve the problem and this is the only thing I found about the subject over the internet.

function super_search_exclude_activate() {
    $striptags = "CREATE FUNCTION fnStripTags( Dirty longtext )
        RETURNS longtext
          DECLARE iStart, iEnd, iLength int;
            WHILE Locate( '<', Dirty ) > 0 And Locate( '>', Dirty, Locate( '<', Dirty )) > 0 DO
                SET iStart = Locate( '<', Dirty ), iEnd = Locate( '>', Dirty, Locate('<', Dirty ));
                SET iLength = ( iEnd - iStart) + 1;
                IF iLength > 0 THEN
                    SET Dirty = Insert( Dirty, iStart, iLength, '');
                END IF;
            END WHILE;
            RETURN Dirty;
    global $wpdb;
    $wpdb->query("DROP FUNCTION IF EXISTS fnStripTags");
register_activation_hook(__FILE__, 'super_search_exclude_activate');

function super_search_exclude_deactivate() {
    global $wpdb;
    $wpdb->query("DROP FUNCTION IF EXISTS fnStripTags");
register_deactivation_hook(__FILE__, 'super_search_exclude_deactivate');

function super_search_exclude_posts_search($search) {
    if (is_search()) {
        global $wpdb;
        if (stripos($search, 'post_content LIKE')) {
            $search = str_replace("{$wpdb->posts}.post_content LIKE", "fnStripTags({$wpdb->posts}.post_content) LIKE", $search);
    return $search;
add_filter('posts_search', 'super_search_exclude_posts_search');

I don't have any error message but the search engine doesn't return a result unless I deactivate the plugin and then I'm back to square one.

What would be the issue?

PS: I created a plugin from this script and activated it in the right admin section.

share|improve this question
up vote 2 down vote accepted

I don't know about the code you are trying to use, personally I would never use a solution that excludes results, it's very possible the item being searched for is in those results and excluding them is only a degradation of service.

Instead you can use the search.php template file to adjust how your search results are displayed, there is a very handy function available in php called strip_tags() which will remove all HTML tags from a string.




 * Template Name: Search Results Page
<section id="primary">
    <div id="content" role="main">
        <?php if (have_posts()): ?>
            <header class="page-header"><?php _e("Search Results"); ?></header>
            <div class="entry-content">
                while (have_posts()): the_post();
                    echo strip_tags(get_the_content());
        <?php endif; ?>
share|improve this answer
No, that's not what I want, thank you. I don't exclude results at all, I'm targeting the posts content instead of the html tags. Users are not supposed to see html tags when searching for anything. Having the code displayed for a front end user can't be anything but a flaw. At least, it's my point of view. – Bachir Messaouri Nov 14 '12 at 1:53
I didn't read the SQL code you posted in detail, I assumed your original title was describing it. The DB hack you are using is not what Databases are intended for and I would still suggest avoiding it. What you want can be done in your templates, unless you don't have access to the templates for some reason. I've updated my answer with more details. – totels Nov 14 '12 at 14:02
@BachirMessaouri, this is the right answer. You complain that the images are displaying in a way that you don't want so clean the markup before you display the content. This is going to faster and probably easier than that MySQL function you have. If something is broken it is with the way the theme prints the results, not with the database query. – s_ha_dum Nov 14 '12 at 14:29
@totels: thank you very much, it did the trick. s_ha_dum: I don't want tags or images in my search results. Using the word "ugly" in my request doesn't mean I want the opposite of ugly which would be "nice (pictures)". Using the word ugly means that tags and images are ruining my template as they are not expected. I don't want neither tags nor pictures, just plain text. – Bachir Messaouri Nov 17 '12 at 4:02

why not create a clear (non html) verson fo post while saving (save_post hook) and place it to additional field of psots tabel (delta tables) and search by this field while searching site (posts_where_request filter hook).

share|improve this answer
This could be done as you suggest it but wouldn't there be to much of data redundancy (as far as I got your point)? – Bachir Messaouri Nov 17 '12 at 4:05
You got my point. but as far as i see you pick solution to strip_html just hefore show it to your user in serch results. but not search not in html. I had use such almost such way whan repalce native wp search by Sphinx search engine search. In that case it was usefull. – qwerty qwerty Nov 17 '12 at 6:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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