admin管理员组文章数量:1125105
My posts have Custom Fields, one of which includes code snippets. I tried esc_html() to present them on the site. this is what I do:
$code = get_post_meta(get_the_id(),'code_snippet')[0];
$code_html = esc_html($code);
echo $code_html;
This doesn't work. It prints the code but whenever there are <
or >
it interprets it as HTML and collapses everything in between as if it was a tag.
However, if I copy the same text from the Custom Field in the dashboard and paste it into the code (so assign $code the pasted string) it does work. And I don't understand why.
Edit: ✅ Solved! It seems like get_post_meta()
is returning an already "stripped" string. I changed how I get the custom field content to
$code = get_post_custom_values($key = 'code_snippet');
echo esc_html($code[0]);
and now it is working.
本文标签: escapingeschtml don39t work on variable but do work on pasted text
版权声明:本文标题:escaping - esc_html don't work on variable but do work on pasted text 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736622597a1945607.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论