How can a user profile dropdown menu be added to a WordPress site using PHP?
To add a user profile dropdown menu to a WordPress site using PHP, you can utilize the `wp_nav_menu()` function along with a custom walker class to generate the dropdown menu. This allows you to display a list of user-specific links in a dropdown format within the site's navigation menu.
// Add user profile dropdown menu to WordPress navigation menu
function add_user_profile_dropdown_menu($items, $args) {
if (is_user_logged_in() && $args->theme_location == 'primary') {
$items .= '<li class="menu-item menu-item-has-children">';
$items .= '<a href="#">Profile</a>';
$items .= '<ul class="sub-menu">';
$items .= '<li><a href="' . get_edit_profile_url() . '">Edit Profile</a></li>';
$items .= '<li><a href="' . wp_logout_url() . '">Logout</a></li>';
$items .= '</ul>';
$items .= '</li>';
}
return $items;
}
add_filter('wp_nav_menu_items', 'add_user_profile_dropdown_menu', 10, 2);