• Resolved Foxer

    (@foxermarcin)


    Hello CA!

    First of all – great plugin. Simple and just works. I currently used {avatar} for the top-level menu for logged and non-logged users, but unfortunately {avatar} do not show any image at all for non-logged users, so my top-level menu-item is blanked for non-logged and showing nothing.

    So here comes my feature request – for {avatar} please put a standard placeholder-avatar image for non-logged users.

    Greetings from Poland,
    Foxer

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Kim L

    (@kimmyx)

    Hi @foxermarcin,

    Thanks for the post. ??

    We’ll forward your request to our development team for consideration.

    Thank you for using User Menus! ??

    Plugin Author Daniel Iser

    (@danieliser)

    @foxermarcin – Hmm, that might be doable within WP already. I think we just use get_avatar, so you could filter it and return a value if user is not logged in.

    I haven’t tested this but looks like it should do the trick, change $default_image to be the url to whatever image you prefer.

    function custom_default_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
        // If there's no logged in user
        if ( ! is_user_logged_in() ) {
            // Path to your default avatar image (change to your actual path)
            $default_image = get_template_directory_uri() . '/path-to-your-default-image.jpg';
    
            // Replace the avatar URL with your default image URL
            $avatar = "<img alt='{$alt}' src='{$default_image}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
        }
    
        return $avatar;
    }
    add_filter( 'get_avatar', 'custom_default_avatar', 10, 5 );
    • This reply was modified 1 year, 1 month ago by Daniel Iser.
    Thread Starter Foxer

    (@foxermarcin)

    @danieliser – thank you, that’s looks promising but unfortunately doesn’t work. Probably because you are using standard get_avatar only in case of logged user. Adding filter to get_avatar for non-logged is worthless until get_avatar is avoided by IF-ELSE statement in the plugin’s code.

    From items.php (line 149+):

    foreach ( $matches as $string ) {
    	if ( ! array_key_exists( $string, Codes::valid_codes() ) ) {
    
    		// If its not a valid code it is likely a fallback.
    		$replace = $string;
    	} elseif ( 0 === $current_user->ID && array_key_exists( $string, Codes::valid_codes() ) ) {
    
    		// If the code exists & user is not logged in, return nothing.
    		$replace = '';
    	} else {
    		switch ( $string ) {
    			case 'avatar':
    				$replace = get_avatar( $current_user->ID, self::$current_item->avatar_size );
    				break;
    [...]
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Avatar for non-logged users’ is closed to new replies.