BuddyDev

Search

[Resolved] Predefined user gallery with display name

  • Participant
    Level: Initiated
    Posts: 13
    Jacob on #43824

    Hello,

    I’m trying to automatically create one gallery for every user.

    I found your code: https://buddydev.com/support/forums/topic/mediapress-predefined-user-galleries/
    which works just fine, I just want each gallery to be named according to the display name of each user. I tried to figure it out by myself, but I’m afraid I need help…

    I also want that the user is able to edit his gallery. It is important that each user can decide if the gallery is public or not, but if possible I want the users not to be able to rename their gallery.

    And I want the images from activity, I mean if a user posts an image in one of the forums, to be automatically added to their gallery.

    I hope you can help me out, thanks in advance!

    Best Jacob

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #43833

    Hello Jacob,

    Welcome to the BuddyDev Forums.

    For creating a gallery with the title as the user display name. Replace the following string:

    
    __( 'Predefined Photo Gallery' )
    

    with

    
    bp_core_get_user_displayname( $user_id )
    

    Note: Please make sure comma(,) remain there

    And to restrict form edit gallery title use the following code:

    
    add_action( 'mpp_user_can_edit_gallery', function ( $can, $gallery, $user_id ) {
    
    	// Do not check if not allowed to edit gallery details.
    	if ( ! $can || $gallery->user_id != $user_id ) {
    		return $can;
    	}
    
    	// Do not allow to change the title.
    	if ( ! empty( $_POST['mpp-gallery-title'] ) && $gallery->title != $_POST['mpp-gallery-title'] ) {
    		$can = false;
    	}
    
    	return $can;
    } );
    
    

    Image from other sources you can use ‘mpp_add_media’ function to add to gallery please have a look at the function supported args.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 13
    Jacob on #43850

    Hello Ravi,

    thanks for your quick response, I tested the changes, but unfortunately no gallery gets created.

    My bp-custom file now looks like this:

    <?php
    // hacks and mods will go here
    
    // Stop gallery creation, only superadmin can
    function mpp_custom_check_gallery_create_permission( $can_do, $component,  $user_id ) {
    	
    	if ( is_super_admin() ) {
    		$can_do = true; //ok super admin can create it
    	} else {
    		$can_do = false;//no one else can
    	}
    	
    	return $can_do;
    }
    
    add_filter( 'mpp_user_can_create_gallery', 'mpp_custom_check_gallery_create_permission', 10, 3 );
    
    //stop editing title
    
    add_action( 'mpp_user_can_edit_gallery', function ( $can, $gallery, $user_id ) {
    
    	// Do not check if not allowed to edit gallery details.
    	if ( ! $can || $gallery->user_id != $user_id ) {
    		return $can;
    	}
    
    	// Do not allow to change the title.
    	if ( ! empty( $_POST['mpp-gallery-title'] ) && $gallery->title != $_POST['mpp-gallery-title'] ) {
    		$can = false;
    	}
    
    	return $can;
    } );
    
    // Restrict gallery deletion
    function mpp_custom_check_gallery_delete_permission( $can, $user_id ){
    	// only superadmin can
    	if ( is_super_admin() ) {
    		$can =  true;
    	} else {
    		$can = false;
    	}
    	
    	return $can;//do not allow it at all
    }
    
    add_filter( 'mpp_user_can_delete_gallery', 'mpp_custom_check_gallery_delete_permission', 10, 2 );
    
    function mpp_custom_auto_create_gallery() {
    	
    	if ( ! bp_is_my_profile() ) {
    		return ;
    	}
    	
    	//make sure that user gallery functions are available
    	if ( ! function_exists( 'mpp_create_gallery' ) ) {
    		return;
    	}
    	
    	
    	$user_id = get_current_user_id();
    		
    	//does this user has predefined gallery?
    	//we use this check to avoid same type of gallery again and again for the user
    	if ( ! get_user_meta(  $user_id, '_mpp_predefined_gallery_photo_1', true ) ) {
    		//No, then we need to create it.
    		
    		//repeat it as you want
    		$gallery_id = mpp_create_gallery( array(
    			'creator_id'	 => $user_id,
    			'title'			 => bp_core_get_user_displayname( $user_id ),
    			'description'	 => '', //add description if any
    			'status'		 => mpp_get_default_status(),//can use any of the status e.g. 'public' , 'private', 'loggedin', 'friends' etc
    			'component'		 => 'members',//for user
    			'component_id'	 => $user_id,//no need to change
    			'type'			 => 'photo', //can change to "audio", "video", "doc"
    		) );
    	
    		//let us keep the id in usermeta to avoid creating it again, right?
    		if ( !  is_wp_error( $gallery_id ) ) {
    			update_user_meta( $user_id, '_mpp_predefined_gallery_photo_1', $gallery_id ); //you can change the key to anything
    
    		}
    		
    	}//end of repeatable block
    	
    	//go ahead, do it for other types too
    	
    	
    	
    }
    add_action( 'bp_template_redirect', 'mpp_custom_auto_create_gallery');
    
    ?>

    Thank you, best

    Jacob

    • This reply was modified 2 years, 9 months ago by Jacob.
  • Participant
    Level: Initiated
    Posts: 13
    Jacob on #43852

    I forgot the last line:

    
    add_action( 'mpp_actions', 'mpp_custom_auto_create_gallery' );
    

    but still no gallery is getting created…

    • This reply was modified 2 years, 9 months ago by Jacob.
    • This reply was modified 2 years, 9 months ago by Jacob.
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #43860

    Hello Jacob,

    Thank you for the acknowledgement. I will check and will update you within 24 hrs.

    Regards
    Ravi

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #43867

    Hello Jacob,

    Please try the following code after removing the existing code:

    https://gist.github.com/raviousprime/6493c7e264154a40e740b0b0e749e996

    It will create a gallery for users with display names, restrict them from deleting them or modifying gallery titles.

    Please make sure the meta key “_mpp_predefined_gallery_photo_1” does not exist for the user you are testing.

    Regards
    RAvi

  • Participant
    Level: Initiated
    Posts: 13
    Jacob on #43868

    Hey Ravi,

    thank you for taking your time!

    I’ll test it later and let you know.

    Best Jacob

  • Participant
    Level: Initiated
    Posts: 13
    Jacob on #43877

    Hey Ravi,

    thank you! The code works just fine! You’re great!

    I guess the problem really was that the meta key “_mpp_predefined_gallery_photo_1” already existed for the users I tested.

    I’m sorry that I have to ask, but where can I check the existing meta keys for certain users and is there a way to delete the metakey if it already exists?

    Thank you, best

    Jacob

  • Participant
    Level: Initiated
    Posts: 13
    Jacob on #43878

    I found a plugin to show and edit the user meta data. Thank you, best Jacob

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #43975

    Hello Jacob,

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

    Regards
    Ravi

The topic ‘ [Resolved] Predefined user gallery with display name’ is closed to new replies.

This topic is: resolved