WordPress Remove Some Annoying Chars
Sometimes when you copy/paste a text from another sources (as Microsoft Word) to WordPress in case the option "paste as text" is not enabled in your editor's settings, some hidden annoying characters will appear, such as
. Here are listed few approaches ho to clear your posts from such characters.
Clear WordPress Output via Functions.php
We could achieve the desired result by filtering the posts content using add_filter( 'the_content' )
within the theme's function.php
file.
function my_function_clear_nbsp ( $content ) {
// $content = preg_replace('/[\x00-\x1F\x7F]/u', '', $content);
$content = str_replace( array( ' ', ' ', "\xc2\xa0" ), ' ', $content);
$content = str_replace( '<p></p> ', '<p> </p>', $content);
return $content;
}
add_filter('the_content', 'my_function_clear_nbsp');
References and more explanations:
- Stack Overflow: How to replace decoded Non-breakable space ( )
Clear WordPress Database via MySQL CLI
Log-in into MySQL command line interface and select the data base of your WordPress site, let's say it is named our_wp_dbase
. For Ubuntu 18.04+ with MySQL socket authentication do something like the follow.
$ sudo mysql [Enter]
mysql> USE our_wp_dbase; [Enter]
Or within phpMyAdmin select the database and then click on the SQL tab and do the following SQL commands/queries.
SELECT * FROM `wp_posts` WHERE (post_content LIKE '% %');
UPDATE `wp_posts` SET post_content = REPLACE(post_content, ' ', ' ') WHERE (post_content LIKE '% %');
The first command will query all instances of the searched string
. The second will replace that string with regular whitespace character. Note wp_
should be changed to your DB prefix.
References: