Support

Account

Home Forums Bug Reports User image field doesn't return what it should – admin

Solved

User image field doesn't return what it should – admin

  • Adding an image field with returned value as an array to user form returns ID when displaying the field in the admin on ACF Tools, Edit Field Group and Add New Field Group pages, any other wordpress admin section returns as an array as its supposed.

    Used to display a custom avatar in the admin panel.

    ACF version 5.5.5

    
    global $current_user;
    $avatar = get_field('avatar', 'user_' . $current_user->ID ); 
    echo '<img src="' . $avatar['url'] . '">';
    
  • Hi @ah

    Could you please share the complete code so I can test it out on my installation?

    Thanks 🙂

  • Sure James

    I made a short version of it.

    Forgot to mention, wp version is 4.7.1

    
    // The field - set in user profile
    // Note that return_format is set to 'array'
    
    	if( function_exists('acf_add_local_field_group') ):
    
    		acf_add_local_field_group(array (
    			'key' => 'group_58018033d8b8a',
    			'title' => 'Profile Avatar',
    			'fields' => array (
    				array (
    					'key' => 'field_5801803c55db7',
    					'label' => 'Avatar',
    					'name' => 'custom_avatar',
    					'type' => 'image',
    					'required' => 0,
    					'conditional_logic' => 0,
    					'return_format' => 'array',
    					'preview_size' => 'thumbnail',
    					'library' => 'all',
    				),
    			),
    			'location' => array (
    				array (
    					array (
    						'param' => 'user_form',
    						'operator' => '==',
    						'value' => 'edit',
    					),
    				),
    			),
    			'position' => 'normal',
    			'style' => 'default',
    			'label_placement' => 'top',
    			'instruction_placement' => 'label',
    			'active' => 1,
    		));
    
    	endif;
    
    	// Stuff happening
    
    	function admin_avatar() { ?>
    
    		<style>
    			#acf-custom-avatar {width: 50%;box-sizing: border-box; padding: 0 12px;}
    			#user-meta {float: right; width: 50%; color: white;}
    		</style>
    
    		<?php
    
    		global $current_user;
    
    		$avatar = get_field('custom_avatar', 'user_' . $current_user->ID ); 
    		
    		echo '<div style="padding-top: 22px;">';
    
    		if( $avatar ) :
    		
    			echo '<img id="acf-custom-avatar" src="' . $avatar['url'] . '">';
    		
    		endif;
    
    		echo '<span id="user-meta">Howdy<br>' . $current_user->display_name . '</span></div>'; 
    
    			
    	}
    
    	add_filter('adminmenu', 'admin_avatar');
  • Thank you James, I’ve got the idea, If you don’t mind Im posting it publicly with a little modification to show thumbnails so it could be of use for anyone with this issue.

    function admin_avatar() { 
    
    		global $current_user;
    		$current_screen = get_current_screen();
    		
    
    		$avatar = get_field('custom_avatar', 'user_' . $current_user->ID ); 
    		
    		if( $avatar ) :
    
    			if( $current_screen->id == 'acf-field-group' || $current_screen->id == 'custom-fields_page_acf-settings-tools' ) :
    
    				$avatar = wp_get_attachment_image_src($avatar, 'thumbnail');		
    				echo '<img src="' . $avatar[0] . '">';
    
    			else :
    
    				echo '<img src="' . $avatar['sizes']['thumbnail'] . '">';
    
    			endif;
    		
    		endif;
    
    			
    	}
    
    	add_filter('adminmenu', 'admin_avatar');
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘User image field doesn't return what it should – admin’ is closed to new replies.