Menu carets (wordpress)

Add carets to menu items with sub menu.

<?php
/**
 * Add carets to menu items with sub menu.
 */
add_filter('walker_nav_menu_start_el', function($item_output, $menu_item) {
    if (in_array('menu-item-has-children', $menu_item->classes)) {
        $menu_caret = '<i class="menu-caret" aria-hidden="true"></i>';
    } else {
        $menu_caret = '';
    }
    $item_output = str_replace(
        array('<a', '/a>'),
        array('<a class="menu-link"', '/a>'.$menu_caret),
        $item_output
    );
    return $item_output;
}, 10, 2);
?>

functions.php

< toolbox