WordPress.org

Ready to get started?Download WordPress

Forums

sanitizing a text field form but keep line breaks (5 posts)

  1. ndh01
    Member
    Posted 4 months ago #

    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

  2. nickohrn
    Member
    Posted 4 months ago #

    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.

  3. ndh01
    Member
    Posted 4 months ago #

    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.

  4. nickohrn
    Member
    Posted 4 months ago #

    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.

  5. ndh01
    Member
    Posted 4 months ago #

    Thanks I will take a look.

Reply

You must log in to post.

About this Topic

Tags

No tags yet.