【问题标题】:Is there a simple way to store html inside a data element?有没有一种简单的方法可以将 html 存储在数据元素中?
【发布时间】:2021-06-09 21:15:59
【问题描述】:
<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 对其进行解码?有没有更好的办法?

【问题讨论】:

  • 也许可以尝试对内容进行base64编码。
  • 与其用所需的内容污染页面,不如考虑在模态打开代码中使用 ajax 请求,以便在实际需要时获取它

标签: php html wordpress


【解决方案1】:

就像您使用的 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 特有的功能)。

【讨论】:

    猜你喜欢
    • 2022-12-07
    • 1970-01-01
    • 2012-02-07
    • 2013-03-26
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多