WordPress Remove Some Annoying Chars

From WikiMLT
Revision as of 20:59, 17 June 2022 by Spas (talk | contribs) (Стадий: 6 [Фаза:Утвърждаване, Статус:Утвърден]; Категория:WordPress)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Some­times when you copy/​​​paste a text from an­oth­er sources (as Mi­crosoft Word) to Word­Press in case the op­tion "paste as text" is not en­abled in your editor's set­tings, some hid­den an­noy­ing char­ac­ters will ap­pear, such as  . Here are list­ed few ap­proach­es ho to clear your posts from such char­ac­ters.

Clear Word­Press Out­put via Functions.php

We could achieve the de­sired re­sult by fil­ter­ing the posts con­tent us­ing add_filter( 'the_content' ) with­in 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>&nbsp;</p>', $content);
    return $content;
}
add_filter('the_content', 'my_function_clear_nbsp');

Ref­er­ences and more ex­pla­na­tions:

Clear Word­Press Data­base via MySQL CLI

Log-in in­to MySQL com­mand line in­ter­face and se­lect the da­ta base of your Word­Press site, let's say it is named our_​​​wp_​​​dbase. For Ubun­tu 18.04+ with MySQL sock­et au­then­ti­ca­tion do some­thing like the fol­low.

$ sudo mysql [Enter]
mysql> USE our_wp_dbase; [Enter]

Or with­in ph­p­MyAd­min se­lect the data­base and then click on the SQL tab and do the fol­low­ing SQL commands/​​​queries.

SELECT * FROM `wp_posts` WHERE (post_content LIKE '%&nbsp;%');
UPDATE `wp_posts` SET post_content = REPLACE(post_content, '&nbsp;', ' ') WHERE (post_content LIKE '%&nbsp;%');

The first com­mand will query all in­stances of the searched string &nbsp;. The sec­ond will re­place that string with reg­u­lar white­space char­ac­ter. Note wp_​​​ should be changed to your DB pre­fix.

Ref­er­ences: