简体   繁体   中英

Quotation marks and apostrophes not displaying in PHP

I am trying to attach an onclick attribute for google analytics to a wordpress nav menu item.

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;
}

I have tried ENT_QUOTES, ENT_NOQUOTES, escaping with a slash, alternating single and double quotation marks, with and without htmlentities() but the HTML is always outputting the HTML characters for quotations like below.

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

How can I ensure this is displayed as per the GA documentation like below - ie, with the URL wrapped in single quotes.

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

Many thanks.

This is not quite the answer I was looking for, but I found a workaround with jQuery.

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

Eliminates all the problems with special characters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM