Assuming that your html is outputted by a wordpress menu sitting in a sidebar, you have to extend the Walker_Nav_Menu Class which is used by menu template functions like wp_nav_menu
. As stated in the reference of wp_nav_menu, you could modify its output like:
class My_DataId_Walker_Nav_Menu extends Walker_Nav_Menu {
// other code found in original Walker_Nav_Menu class
// changing output of li elemens
$output .= $indent
. '<li id="nav-menu-item-' . $item->ID . '" '
. 'data-id="' . $item->ID . '" '
. 'class="' . $depth_class_names . ' ' . $class_names . '">';
// other code found in original Walker_Nav_Menu class
}
// in the template file, output menu with custom walker class
wp_nav_menu(
array(
'theme_location' =>'primary', // your menu position
'walker' => new My_DataId_Walker(),
'depth' => 0
)
);