BuddyDev

Search

[Resolved] Custom cover photo by role

  • Participant
    Level: Enlightened
    Posts: 41
    la on #52331

    I decided not to use Youzify. So now I’m back to just BP and BuddyDev.

    I have created a new snippet with the code you sent and it works great for the admin role. However, I have many custom roles which don’t match the permissions driving editor, contributor etc.

    https://buddydev.com/support/forums/topic/xprofile-plugin-different-from-what-is-offered-in-buddy-press-natively/

    Could you give me a snippet where it could just be the name of the role? Role ABC = this-image-url? Not be based on permissions/capabilities? i have about 30 custom roles I’d like to customize it by but they all have similar permissions/capabilities.

    Thanks

  • Participant
    Level: Enlightened
    Posts: 41
    la on #52332

    Also do you have one which is just for all default cover photos, not role dependent?

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2944
    Ravi on #52335

    Hello La,

    Thank you for posting. Please let me know your exact requirements so that I can help. Are you looking for role-based default cover images or any single default cover image that would apply to all the users without a cover image? Please let me know.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 41
    la on #52338

    That would be fantastic, thank you so much.

    Role based: I have ACF and about 30 different custom roles. I’d like to be able to assign a default cover photo to each role based on the name of the role, not on the capabilities of the role.

    As no one would ever see a cover photo unless they had registered thus assigning a role to them, I think just a role name based default cover photo covers the second option you mentioned.

    Thank you so much for the help! 🙂

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2944
    Ravi on #52340

    Hello La,

    Thank you for the acknowledgement. Please try the following code:

    
    
    /**
     * Default cover based on role
     *
     * @param array $settings Cover image settings.
     *
     * @return array
     */
    function buddydev_set_custom_default_profile_cover( $settings = array() ) {
    
    	if ( ! bp_is_user() ) {
    		return $settings;
    	}
    
    	$user = get_user_by( 'id', bp_displayed_user_id() );
    
    	if ( ! $user ) {
    		return $settings;
    	}
    
    	if ( in_array( 'administrator', $user->roles, true ) ) {
    		// Users with administrator role.
    		$settings['default_cover'] = 'http://example.com/cover-admin.jpg';
    	} elseif ( in_array( 'editor', $user->roles, true ) ) { //editor
    		// Users with editor role.
    		$settings['default_cover'] = 'http://example.com/cover-editor.jpg';
    	} elseif ( in_array( 'author', $user->roles, true ) ) { //author
    		// Users with author role.
    		$settings['default_cover'] = 'http://example.com/cover-author.jpg';
    	} elseif ( in_array( 'contributor', $user->roles, true ) ) {
    		// Users with contributor role.
    		$settings['default_cover'] = 'http://example.com/cover-contributor.jpg';
    	} elseif ( in_array( 'subscriber', $user->roles, true ) ) {
    		// Users with subscriber role.
    		$settings['default_cover'] = 'http://example.com/cover-subscriber.jpg';
    	} else {
    		$settings['default_cover'] = 'http://example.com/cover.jpg';
    	}
    
    	return $settings;
    }
    add_filter( 'bp_before_members_cover_image_settings_parse_args', 'buddydev_set_custom_default_profile_cover', 40 );
    
    

    Let me know if it helps or not.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 41
    la on #52343

    That is so perfect, thank you! It works great 🙂
    One more question, is there one that could be “if no role = cover specified, this file is always the fallback default” so if I haven’t specified it for a role and they haven’t changed it, there would always be a fallback?

    Thank you so much.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2944
    Ravi on #52345

    Hello La,

    Thank you for the acknowledgement. I am glad that it worked. Just comment or delete the following code:

    
    else {
    		$settings['default_cover'] = 'http://example.com/cover.jpg';
    	}
    
    

    With this step, no default cover will apply if the user does not have the mentioned role in the conditions.

    Please check.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 41
    la on #52350

    Ah I see you already had it built in, that’s what I do want, so I’ll leave it there. I want there to be a fallback if no role is specified.

    Great, thank you! 🙂

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2944
    Ravi on #52352

    Hello La,

    Thank you for the acknowledgement. I am glad that I could help.

    Regards
    Ravi

The topic ‘ [Resolved] Custom cover photo by role’ is closed to new replies.

This topic is: resolved