BuddyDev

Search

[Resolved] Hiding empty member profile cover image

Tagged: 

  • Participant
    Level: Initiated
    Posts: 5
    Tony on #1551

    Hi… am testing the new member profile cover image feature available in BuddyPress 2.4.0.

    Now profiles have an empty grey box unless user has uploaded a cover image. This can be removed by turning off Cover image uploads in Settings>BuddyPress>Settings settings panel. But this makes it an all or nothing feature.

    I would like to retain the option for users to upload a cover image while not displaying the empty grey box for those who do not.

    I am trying to work out how to do this. I have looked at buddypress/members/single/cover-image-header.php but cannot see how best to do this – especially how to do it in a way that allows BP to be updated without losing a hack.

    Any advice will be much welcomed.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #1552

    Hi Tony,
    Welcome to BuddyDev and thank you for asking this.

    There are multiple ways to do it.

    1. Not that good but you will not need to touch any template file.
    We can put the following in our bp-custom.php

    
    /**
     * Check if user has uploaded cover
     * 
     * @param type $user_id
     * @return boolean
     */
    function buddydev_user_has_uploaded_cover( $user_id ) {
    	
    	$cover_image = bp_attachments_get_attachment( 'url', array(
    			'object_dir' => 'members',
    			'item_id'    => $user_id,
    		) );
    	
    	if( ! empty( $cover_image ) ) {
    		return true;
    	}
    	
    	return false;
    }
    /**
     * We use it to filter on 'bp_is_profile_cover_image_active' to enable/disable the cover display
     * 
     * @param type $enabled
     * @return boolean
     */
    function buddydev_check_for_uploaded_cover( $enabled ) {
    	
    	if ( ! bp_is_user() ) {
    		return $enabled;
    	}
    	
    	$user_id = bp_displayed_user_id();
    	
    	if( buddydev_user_has_uploaded_cover( $user_id ) ){
    		return $enabled;
    	}
    	return false;
    	//now, we need to check if the user has uploade image or not
    }
    
    // a superbad hack since BuddyPress does not provide any specific hook
    function buddydev_enable_cover_hook() {
    	add_filter( 'bp_is_profile_cover_image_active', 'buddydev_check_for_uploaded_cover' );
    }
    add_action( 'bp_before_member_home_content', 'buddydev_enable_cover_hook' );
    
    //disable it to avoid any sideecct on menus
    //and we remove it immediately after header is loaded
    function buddydev_disable_cover_hook() {
    	if( has_filter( 'bp_is_profile_cover_image_active', 'buddydev_check_for_uploaded_cover' ) ) {
    		remove_filter( 'bp_is_profile_cover_image_active', 'buddydev_check_for_uploaded_cover' );
    	}
    }
    add_action( 'bp_before_member_header', 'buddydev_disable_cover_hook' );
    
    

    This works with template pack and will work with any theme.

    I personally don’t recommend this approach as it is very hakish. The reason we do it like above is BuddyPress template pack or BuddyPress is not giving us a separate function/hook to disable loading the cover-header template only. It uses same function everywhere, If we do filter on it everywhere, a user will not be able to see the upload cover photo link.

    The better approach is to use only one function from my above code

    
    
    /**
     * Check if user has uploaded cover
     * 
     * @param type $user_id
     * @return boolean
     */
    function buddydev_user_has_uploaded_cover( $user_id ) {
    	
    	$cover_image = bp_attachments_get_attachment( 'url', array(
    			'object_dir' => 'members',
    			'item_id'    => $user_id,
    		) );
    	
    	if( ! empty( $cover_image ) ) {
    		return true;
    	}
    	
    	return false;
    }
    
    

    and then look in members/single/home.php for this line

    
    		<?php
    		/**
    		 * If the cover image feature is enabled, use a specific header
    		 */
    		if ( bp_displayed_user_use_cover_image_header() ) :
    			bp_get_template_part( 'members/single/cover-image-header' );
    		else :
    			bp_get_template_part( 'members/single/member-header' );
    		endif;
    		?>
    
    

    and change that to

    
    
    		<?php
    		/**
    		 * If the cover image feature is enabled, use a specific header
    		 */
    		if ( buddydev_user_has_uploaded_cover( bp_displayed_user_id() ) ) :
    			bp_get_template_part( 'members/single/cover-image-header' );
    		else :
    			bp_get_template_part( 'members/single/member-header' );
    		endif;
    		?>
    
    

    That is much more meaningful and clean solution.

    Hope that helps.

    Please do let me know how it goes with you.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    Tony on #1557

    Brash Singh – thank you so very much for taking the trouble to make such a detailed reply and to provide the code suggestions.

    Despite my feeling I am not skilled in this area, I was able to understand all you supplied – it was very clearly explained.

    Although your solution (1) is more of a hack, it does have the advantage of, hopefully, surviving BuddyPress updates, so I tried that and it worked! It is looking good: http://ovni-owners.org.uk/members/antipole/

    It did break some other custom filtering of profile dada that I had already implements, but I have been able to fix that up.

    I doubt that I will be alone in wanting this. I think it would be a good BuddyPress enhancement to include this as standard, or, at least, provide a switch in the settings panel. Can you feed that into the development channels?

    thanks once again for your help.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #1558

    Hi Tony,
    Thank you. I am glad I could help 🙂
    Will be certainly opening a ticket on BuddyPress trac in a day or 2 regrading the same as it will be very useful if there was simple way to do it.

    Marking this topic as resolved now.

    Thank you
    Brajesh

The topic ‘ [Resolved] Hiding empty member profile cover image’ is closed to new replies.

This topic is: resolved