最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sanitization - Why is sanitize_text_field() selectively trimming data?

programmeradmin1浏览0评论

In my plugin people can add placeholders that are substituted later in the workflow.

A typical placeholder might look like %name%, %address% %postal_code%

However in the database sanitize_text_field saves it as %name%, dress% %postal_code% - it strips the leading % from only one instance. I can workaround it by using esc_html, but am curious about what is going on here.

Can anyone explain it?

In my plugin people can add placeholders that are substituted later in the workflow.

A typical placeholder might look like %name%, %address% %postal_code%

However in the database sanitize_text_field saves it as %name%, dress% %postal_code% - it strips the leading % from only one instance. I can workaround it by using esc_html, but am curious about what is going on here.

Can anyone explain it?

Share Improve this question edited Feb 16 at 0:16 Steve asked Feb 15 at 22:51 SteveSteve 2991 gold badge4 silver badges15 bronze badges 2
  • I would note that while this is tagged as escaping, that function is a sanitising function not an escaping function, and should never be used to escape. Sanitising and escaping are two very different things. Likewise you should not be using esc_html to sanitise data on its way into the database, esc_html is an escaping function not a sanitising function – Tom J Nowell Commented Feb 15 at 23:38
  • Thanks, I couldn't spot the right tag, and had to use one, that caught my eye :P – Steve Commented Feb 16 at 0:16
Add a comment  | 

1 Answer 1

Reset to default 1

Can anyone explain it?

The official docs for that function say it strips percent encoded characters.

  • Checks for invalid UTF-8,
  • Converts single < characters to entities
  • Strips all tags
  • Removes line breaks, tabs, and extra whitespace
  • Strips percent-encoded characters

https://developer.wordpress/reference/functions/sanitize_text_field/

It looks like it's stripped out %ad and it has nothing to do with instances or leading/trailing, that function doesn't understand that it's a CSV etc, % probably isn't the best character to use for this.

Why just the %ad though? In this case it's the URL encoded value for ­ aka a soft hyphen, or &shy;.

See https://www.w3schools/tags//ref_urlencode.asp for a more comprehensive table.

发布评论

评论列表(0)

  1. 暂无评论