I like to add a class and an onClick event to a single WordPress menu link.
My code is based on an example given by WordPress here:
function add_specific_menu_atts( $atts, $item, $args ) {
$menu_items = array(8071);
if (in_array($item->ID, $menu_items)) {
$atts['onClick'] = 'ml('show', 'OUN6T5', true)';
$atts['class'] = 'ml-onclick-form button';
}
return ($atts);
}
add_filter( 'nav_menu_link_attributes', 'add_specific_menu_atts', 10, 3 );
It all works fine so far except for the onClick event (a pop-up showing a newsletter form powered by MailerLite).
This is the output I want to achieve:
<a href="#" onClick="ml('show', 'OUN6T5', true)" class="ml-onclick-form button">Join My Newsletter</a>
But the single quotes in the onClick event are replaced by HTML entites '
.
Instead of onClick="ml('show', 'OUN6T5', true)"
I get
onClick="ml('show', 'OUN6T5', true)"
Using html_entity_decode like this didn’t help:
$atts['onClick'] = html_entity_decode(ml('show', 'OUN6T5', true));
I think that I probably need to apply html_entity_decode to return ($atts);
, but I don’t know how to add html_entity_decode to an array.
Maybe I’m also wrong, and another approach would be better. I hope someone can point me into the right direction.