0

I have   tags as spaces in autocreated posts in Wordpress and trying to replace them before posting/saving. Text sample below:

This is an Example of how my text looks

Believe this code placed in "functions.php" should replace the tags with spaces

add_filter( 'wp_insert_post_data' , 'nbsp_remover' , '99', 2 );

function nbsp_remover($data) {
    $pattern = '/\&nbsp\;/';
    $data['post_content'] = preg_replace($pattern, ' ', $data['post_content']);
    return $data;
}

But this doesn't work.

  • I tested the regex and it works.
  • when I change the pattern to a word in the text it works

But not with the   tags in the html...

Any idea?

EDIT 1

when dumping $data as suggested using var_dump($data['post_content']); die;

creating a new post manually, opens blank window with:

array(21) { ["post_author"]=> int(1) ["post_date"]=> string(19) "2021-01-26 17:15:29" ["post_date_gmt"]=> string(19) "0000-00-00 00:00:00" ["post_content"]=> string(0) "" ["post_content_filtered"]=> string(0) "" ["post_title"]=> string(10) "Auto Draft" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(10) "auto-draft" ["post_type"]=> string(4) "post" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(0) "" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2021-01-26 17:15:29" ["post_modified_gmt"]=> string(19) "0000-00-00 00:00:00" ["post_parent"]=> int(0) ["menu_order"]=> int(0) ["post_mime_type"]=> string(0) "" ["guid"]=> string(0) "" }

EDIT 2 This with \xA0or ' ' did not work

add_filter( 'wp_insert_post_data' , 'nbsp_remover' , '99', 2 );

function nbsp_remover($data) {
    return str_replace('\xA0', '', $data); 
}
13
  • The text sample is the what you have in $data? Can you add the output of var_dump($data['post_content']); before your preg_replace? Commented Jan 26, 2021 at 16:20
  • I forgot to add I'm an beginner at WP and this is first time ever trying a php script so not sure what you are suggesting @ChinLeung Commented Jan 26, 2021 at 16:25
  • Add var_dump($data['post_content']); die; as the first line in your nbsp_remover function, and try to create a new post. It will display the content of $data['post_content'], and copy paste it into your question. :) Commented Jan 26, 2021 at 16:29
  • I added the line - Now when I try to add new post manually i get a white screen stating: - string(0) "" - Commented Jan 26, 2021 at 16:37
  • It is highly probably all your   are already turned into \xA0, try return str_replace('\xA0', '', $data);. BTW, you are not using the $postarr variable in the function. Commented Jan 26, 2021 at 16:37

1 Answer 1

0

Ok, I solved my issue by circumventing it...

As noted above I couldn't find a way to remove the   tags from the post content.

I was able to edit the posts before they got send and read by wordpress. So I replaced all spaces with a special character before the post got created in WP and than used the regex code to replace that character with spaces.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.