0

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.

  • Have you tried using double quotes for the outer quotes? $mystring = "'http://example.com/your-link'"; You wouldn't need to escape them then either. Also, have you tried not using htmlentities? Do you get the same behaviour? – Ajaypayne Sep 12 '18 at 17:54
  • `'` is an apostrophe. It's supposed to be escaped for HTML. If you don't want it escaped, don't use `htmlentities()`. – miken32 Sep 12 '18 at 17:59
  • 1
    I'm not sure how to *solve* your problem, but I know that the *cause* of it is on [this line](https://github.com/WordPress/WordPress/blob/6fd8080e7ee7599b36d4528f72a8ced612130b8c/wp-includes/class-walker-nav-menu.php#L203): wordpress applies its `esc_attr` function to your attribute value. – Stratadox Sep 12 '18 at 18:06
  • Tried with and without htmlentities(), same result. – Craig Evans Sep 12 '18 at 18:06
  • @miken32 Using ENT_NOQUOTES would in theory make sure the quotes are ignored by `htmlentities`. See my previous comment as for why that does not matter here. – Stratadox Sep 12 '18 at 18:07
  • I have tried with and without htmlentities(), I get the same results. Have also tried replacing outer quotes with double quotes, and separately making the inner quotes double, and escaping inner quotes (double and single) with a backslash, but still no joy. – Craig Evans Sep 12 '18 at 18:09
  • @Stratadox good point, I hadn't noticed he was using Wordpress. Regardless, attribute values are supposed to be escaped, so the JS should still execute just fine. – miken32 Sep 12 '18 at 18:09
  • `html_entity_decode` is your friend :) – Herwig Sep 12 '18 at 19:01

1 Answers1

0

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