【问题标题】:Quotation marks and apostrophes not displaying in PHPPHP 中不显示引号和撇号
【发布时间】:2019-02-17 10:22:52
【问题描述】:

我正在尝试将 google 分析的 onclick 属性附加到 wordpress 导航菜单项。

add_filter( 'nav_menu_link_attributes', 'wpse121123_contact_menu_atts', 10, 3 );
function wpse121123_contact_menu_atts( $atts, $item, $args )
{
  // The ID of the target menu item
  $menu_target = 6384;

  $mystring = '\'http://example.com/your-link\'';

  // inspect $item
  if ($item->ID == $menu_target) {
    $atts['onclick'] = 'return gtag_report_conversion(' . htmlentities($mystring, ENT_NOQUOTES) . ');';
  }
  return $atts;
}

我尝试过 ENT_QUOTES、ENT_NOQUOTES、用斜杠转义、交替使用单引号和双引号、带和不带 htmlentities(),但 HTML 总是输出 HTML 字符以进行如下引用。

<a title="link" href="#" class="nav-link" onclick="return gtag_report_conversion(&#039;http://example.com/your-link&#039;);">Link</a>

我怎样才能确保按照下面的 GA 文档显示它 - 即,用单引号括起来的 URL。

<a title="link" href="#" class="nav-link" onclick="return gtag_report_conversion('http://example.com/your-link');">Link</a>

非常感谢。

【问题讨论】:

  • 你试过用双引号作为外引号吗? $mystring = "'example.com/your-link'";你也不需要逃避它们。另外,您是否尝试过不使用 htmlentities?你有同样的行为吗?
  • &amp;#039; 是撇号。它应该为 HTML 转义。如果您不希望它转义,请不要使用htmlentities()
  • 我不确定如何解决您的问题,但我知道它的原因this line:wordpress 应用它esc_attr 函数到你的属性值。
  • 尝试使用和不使用 htmlentities(),结果相同。
  • @miken32 理论上使用 ENT_NOQUOTES 可以确保 htmlentities 忽略引号。请参阅我之前的评论,了解为什么这无关紧要。

标签: php wordpress gtag.js wp-nav-menu-item


【解决方案1】:

这不是我一直在寻找的答案,但我找到了使用 jQuery 的解决方法。

jQuery('#menu-item-6384 > a').attr("onclick", "return gtag_report_conversion('https://www.mylink.com/conversion/');");

消除所有特殊字符的问题

【讨论】:

  • 很高兴您找到了更好的解决方案,我最初的建议要糟糕得多。
  • 感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 2018-09-21
  • 2023-03-06
相关资源
最近更新 更多