0

Anyone have a tip on how to prevent Wordpress from saving utf8 space characters (= hex C2 A0) in DB by replacing with space \x20 ?

Is there a hook that can be used to do a replace before saving to tables?

Background:   looks like any other "space" character to the eye in editors (Notepad, Notepad++, Wordpress etc.) but actually hides itself in 2-byte code (\xc2\xa0) if you view though a hex editor.

Problem: css word-wrap causes sentence not to wrap as they should as it knows it's a "non-breaking space".

User experience: When someone does copy+paste from another source (website, word document etc.) sometimes the original space is a   and the user doesn't see or know this as they're not that tech focused as we are.

Anyone has an idea or proposal for solution here?

Thx

5
  • 2
    there's a lot more than non-breaking spaces that can end up in pasted content. I would train users to get in the habit of using the paste as plain text button, which will strip all formatting.
    – Milo
    Nov 13, 2014 at 17:25
  • Thx @Milo - it's a great idea. The reason I opted for managing the issue with space is that it's less obvious as you cannot see it even in preview. Most other formatting issues can be previewed. Nov 14, 2014 at 18:10
  • @Milo non–breaking space isn't affected by plain text paste (I just tested), it's not formatting per se.
    – Rarst
    Nov 14, 2014 at 19:28
  • @Rarst - I just tested and it is.
    – Milo
    Nov 14, 2014 at 19:33
  • Curious. I actually get strange reverse behavior — it stays as special symbol with plain text paste mode active (button pressed), but turns into normal space when it's inactive (button unpressed). Clear formatting button doesn't convert it either.
    – Rarst
    Nov 14, 2014 at 19:59

1 Answer 1

2

Both initial creation and updates for the posts pass through wp_insert_post_data (among other filters). You can modify post_content item in array passed to make the replacement you need, before it proceeds on to be saved in database.

Update Code pasted from comments

add_filter( 'wp_insert_post_data', 'rm_wp_insert_post_data', '99', 2 );
function rm_wp_insert_post_data ( $data , $postarr ) {
   return str_replace("\xc2\xa0", " ", $data); 
}
3
  • Thx. I'll try this. Nov 13, 2014 at 16:21
  • Thx @Rarst It solved my problem by using this hook. add_filter( 'wp_insert_post_data', 'rm_wp_insert_post_data', '99', 2 ); function rm_wp_insert_post_data ( $data , $postarr ) { return str_replace("\xc2\xa0", " ", $data); } Nov 13, 2014 at 16:36
  • @MattiasFjalestad Then please mark the answer as solution.
    – kaiser
    Nov 13, 2014 at 19:23

Your Answer

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

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