Inserting PHP before a menu element, while using the inbuilt menu function wp_nav_menu()

The easiest way to do that would be to inline some CSS in your php template. then you could replace that hardcoded image URL with the users avatar like background-image: url("' . echo $avatarURL . ");

You will definitely need to fix your snippet to get the avatar as well though. currently you’re returning the user ID and then trying to grab the avatar. anything after a return statement is unreachable code.

function get_current_user_avatar_url() {
    if ( ! function_exists( 'get_current_user_id' ) ) {
        return false;
    }
    $user = get_current_user_id();

    return get_avatar_url($user);
}

then define $avatarURL like $avatarURL = get_current_user_avatar_url();

Otherwise you’re getting into writing a custom nav walker, which is much more complicated.

Leave a Comment