Support » Plugins » Hacks » sanitizing a text field form but keep line breaks

  • I have been using sanitize_text_field() to sanitize a text field in a form. I need to keep the line breaks in the form so I can use nl2br to place each line break on a new line when displaying back the data.

    Any ideas on how to safely sanitize and yet preserve line breaks?

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • What are you trying to sanitize out of the field? sanitize_text_field forcefully strips out all new lines and tabs with a regex.

    You may be better of using wp_kses, elements depending on your specific use case.

    I want to make sure that html/SQL/php/js etc can’t be submitted and executed. I do however want the line breaks to remain.

    Well, you can’t use sanitize_text_field because it removes the line breaks (and there’s no way to tell it not to). I’d recommend looking at the wp_kses family of functions and seeing if one of them fits your use case.

    Thanks I will take a look.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘sanitizing a text field form but keep line breaks’ is closed to new replies.