Site logo

Archived topic

onclick= in a menu item

3 replies · Started by Adrien on December 1, 2022

Viewing posts 1–4 of 4

Dear GP Team,

I've successfully added onclick=Gleap.open(); to a button adding this code into my page as HTML block: Chat with us

However, Im trying to add this to a menu item: onclick="Gleap.open();
I can't find a way to do it.

Would greatly appreciate your help. Thank you.

Hi Adrien,

Which specific menu item are you planning to add it to?

You can use WordPress filter - nav_menu_link_attributes. Reference: https://developer.wordpress.org/reference/hooks/nav_menu_link_attributes/

Sample code:

function add_class_to_non_top_level_menu_anchors( $atts, $item, $args, $depth ) {
	$menu_items = array(34,395437);
	if(in_array($item->ID, $menu_items)){
		$atts['onClick'] = 'teGleap.open()st';
	}

	
	return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_class_to_non_top_level_menu_anchors', 10, 4 );

This code adds your onClick attribute to Menu items with IDs 34 and 395437.

You may replace these values for your reference.

You can find the ID of a menu item by right-clicking on it, inspecting it, the going to the Elements tab. Example: https://share.getcloudapp.com/d5uybLEA

Thanks a lot Fernando. It worked perfectly.

You're welcome, Adrien!

This archived topic is closed to new replies.