<a class="open-modal" href="#special-modal"
data-html="<p>HELLO</p> <strong>dddddddddddddddddd</strong><a
href=" http:="" www.google.com"="">asdas</a>
尝试存储一些简单的 html,结果 html 损坏,有没有办法使用 php 更改字符串以便能够在数据元素中存储任何 html?
<a class="open-modal" href="#special-modal" data-html="<?= get_post_meta($post->ID, '_newproduct_data-html', false)[0] ?>"><?= esc_html_e('Modal', 'blacksmith'); ?></a>
我是否需要在 php 中对 get_post_meta 的结果进行编码,然后在模式打开时使用 php 对其进行解码?有没有更好的办法?
答案 0 :(得分:0)
就像您使用的 e_html_e()
函数一样,还有一个 esc_attr()
函数,专门用于转义 HTML 属性值。
<a class="open-modal" href="#special-modal" data-html="<?= esc_attr(get_post_meta($post->ID, '_newproduct_data-html', false)[0]) ?>"><?= esc_html_e('Modal', 'blacksmith'); ?></a>
注意:esc_attr()
是 WordPress 特定的函数(因为问题已经在使用其他 WP 特定的函数)。