Support » Fixing WordPress » Remove/hide specific word from excerpt?

  • Resolved catmaniax

    (@catmaniax)


    Hello everyone!

    As the title says, I’d like to know if there’s a way (some code to put in functions.php maybe) to remove or just hide a specific word from posts’ excerpts.

    Please let me know.
    Thanks!

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    You could use https://codex.wordpress.org/Plugin_API/Filter_Reference/get_the_excerpt to get the excerpt and do a strreplace to remove the unwanted words.

    Thanks for the reply.

    I don’t write anything in the excerpt section of the backend, it just grabs the content to display as the excerpt.

    Can you please explain how I can easily remove the unwanted words, because I did not understand.

    Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    Something like this… NOTE: THIS IS NOT TESTED ad may or may not work. YOU MAY HAVE TO DEBUG.

    function my_clean_excerpt( $excerpt ) {
       $badwords = array( 
          'word1',
          'word2',
          'word3',
        );
       foreach ( $badwords as $badword ) {
           $excerpt = str_replace( $badword, '', $excerpt);
           }
       return $excerpt;
    }
    add_filter( 'get_the_excerpt', 'my_clean_excerpt' );

    That did the trick quite nicely!

    Thank you.

    One more question.
    Any idea how to add … to the last word of the excerpt?

    • This reply was modified 2 years, 3 months ago by catmaniax.
    Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    $excerpt .= " just one more thing... "
    return $excerpt;

    I get a parse error when I paste above code along with the first code you posted 🙁

    btw, thanks again!

    Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    If you can’t debug the code, show me the code you are using via pastebin.com or gist.github.com

    This is why I don’t like giving people code!

    I didn’t use any different code, I just don’t know how to make the two codes you gave me both work.

    function my_clean_excerpt( $excerpt ) {
       $badwords = array( 
          'word1',
          'word2',
          'word3',
        );
       foreach ( $badwords as $badword ) {
           $excerpt = str_replace( $badword, '', $excerpt);
           }
       return $excerpt;
    }
    add_filter( 'get_the_excerpt', 'my_clean_excerpt' );
    
    $excerpt .= " just one more thing... "
    return $excerpt;
    
    Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    No, no, no.

    Put $excerpt .= " just one more thing... " above return $excerpt INSIDE the function and delete those last two lines.

    • This reply was modified 2 years, 3 months ago by Steve Stern.

    But wouldn’t doing that replace the function of not showing the bad words?

    Sorry for asking again, but could you please post the whole correct code including both of those functions?

    Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    No. Look up what the “dot equal” operator does in PHP.

    function my_clean_excerpt( $excerpt ) {
       $badwords = array( 
          'word1',
          'word2',
          'word3',
        );
       foreach ( $badwords as $badword ) {
           $excerpt = str_replace( $badword, '', $excerpt);
           }
       return $excerpt . ' one more thing... ';
    }
    add_filter( 'get_the_excerpt', 'my_clean_excerpt' );

    Thank you.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Remove/hide specific word from excerpt?’ is closed to new replies.