admin管理员组文章数量:1122832
I have a meta field which gets displaed in the admin panel look like:
$field = get_post_meta($postid, 'field_name', true); ?>
<input type="text" name="field_name" value="<?php echo (!empty($field)) ? $field : ''; ?>" />
And usually whenever I save these sorts of things I use sanitize_text_field()
which I thought that was enough, until today... Today I realized that it doesn't properly encode any html entities, such as quotations. For example: Say the user enters into my text field
Matt said "Jet fuel can't melt steel beams..."
Which can be sanitized using the function above but whenever echo'ed back out to the user I get
Matt said
The input looks like:
<input type="text" name="field_name" value="Matt said " Jet fuel can't melt steel beams" />
I can use htmlspecialchars()
whenever saving my meta but then that defeats the purpose. Why would I ever use sanitize_text_field()
over strip_tags( htmlspecialchars() )
?
I have a meta field which gets displaed in the admin panel look like:
$field = get_post_meta($postid, 'field_name', true); ?>
<input type="text" name="field_name" value="<?php echo (!empty($field)) ? $field : ''; ?>" />
And usually whenever I save these sorts of things I use sanitize_text_field()
which I thought that was enough, until today... Today I realized that it doesn't properly encode any html entities, such as quotations. For example: Say the user enters into my text field
Matt said "Jet fuel can't melt steel beams..."
Which can be sanitized using the function above but whenever echo'ed back out to the user I get
Matt said
The input looks like:
<input type="text" name="field_name" value="Matt said " Jet fuel can't melt steel beams" />
I can use htmlspecialchars()
whenever saving my meta but then that defeats the purpose. Why would I ever use sanitize_text_field()
over strip_tags( htmlspecialchars() )
?
1 Answer
Reset to default 0You need to escape data while displaying also. Use esc_attr
in your case. Try this:
<input type="text" name="field_name" value="<?php echo (!empty($field)) ? esc_attr( $field ) : ''; ?>" />
Check this for full documentation - https://codex.wordpress.org/Data_Validation
本文标签: validationProper Way to Sanitize Meta Input
版权声明:本文标题:validation - Proper Way to Sanitize Meta Input 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308061a1933527.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论