BuddyDev

Search

[Resolved] Help w/ auto-create gallery code

  • Participant
    Level: Initiated
    Posts: 12
    Panos on #14748

    Hi all,
    I’m customizing MediaPress according to the needs of the site I’m building. Just to mention that the ability to make pictures show only for friends is such a great feature! I will need to fix an issue that I noticed with that but one thing at a time.
    First, I’m having a single public gallery created automatically at user registration. I found the way to do it from other posts on this forum but the code I ended up with is not working. Can you tell me if everything looks good?
    Thank you in advance.

    function mpp_custom_create_gallery($user_id) { 
    	
    	if ( ! function_exists( 'mpp_is_user_gallery_component' ) ) { 
    		return;
    	}
    	
    	$gallery_id = mpp_create_gallery( array(
    		'creator_id'	 => $user_id,
    		'title'			 => $user_id,
    		'description'	 => '',
    		'status'		 => 'public',
    		'component'		 => 'members',
    		'component_id'	 => $user_id,
    		'type'			 => 'photo'
    	) );
    	
    	if ( $gallery_id ) {					
    		mpp_update_wall_gallery_id( array(
    			'component'		=> 'members',
    			'component_id'	=> $user_id,
    			'media_type'	=> 'photo',
    			'gallery_id'	=> $gallery_id
    		) );
    	}
    }
    
    add_action( 'user_register', 'mpp_custom_create_gallery', 10, 1 );
  • Participant
    Level: Initiated
    Posts: 12
    Panos on #14751

    I added an echo to output the $user_id, to make sure it passes and it is.
    I also added an echo to give me the $gallery_id right after “$gallery_id = mpp_create_gallery( array( etc.” and it’s giving me the id which probably means that the gallery is created (I think/hope). So why is it not showing up on the user’s profile? Is there an extra step I’m missing?
    Thanks

  • Participant
    Level: Initiated
    Posts: 12
    Panos on #14752

    I also tried the below code from one of Brajesh’s posts. Still nothing.

    function mpp_custom_auto_create_gallery($user_id) {
    	
    	//make sure that user gallery functions are available
    	if ( ! function_exists( 'mpp_create_gallery' ) ) {
    		return;
    	}
    	
    	//repeatable block
    		$gallery_id = mpp_create_gallery( array(
    			'creator_id'	 => $user_id,
    			'title'			 => 'Pictures',
    			'description'	 => '', //add description if any
    			'status'		 => 'public',//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, '_Pictures', $gallery_id ); //you can change the key to anything
    
    		}
    	//end of repeatable block
    	
    	//repeatable block
    		$gallery_id = mpp_create_gallery( array(
    			'creator_id'	 => $user_id,
    			'title'			 => 'Private Album',
    			'description'	 => '', //add description if any
    			'status'		 => 'friendsonly',//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, '_Private_Album', $gallery_id ); //you can change the key to anything
    
    		}
    	//end of repeatable block
    	
    	//echo $user_id . '   ' . $gallery_id;
    	
    }
    
    add_action( 'user_register', 'mpp_custom_auto_create_gallery', 10, 1 );
  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #14753

    Hi Panos,
    Welcome to BuddyDev.
    Your code is correct. My guess is the problem lies with the hook it is attached to.

    MediaPress is state is setup on init action and most probably the user_register is getting called early.

    At that time, MediaPress will not have the type/status/component set and that’s why the gallery might not be created.

    I will suggest hooking to any action that comes after init. I believe your goal is to optimize the database query by hooking to account creation.

    It will be fine even if you hooked to ‘mpp_actions’ and stored the id in user meta. BuddyPress uses the cached meta for the displayed user/logged user and members loop users. So, your code won’t be causing any extra database query.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 12
    Panos on #14757

    Hi Brajesh,

    Thanks for the reply. So yes, if I hook it up to ‘mpp_actions’ it works fine and you’re right, I’m doing it to optimize database query. I didn’t quite get the part where I store the id in user meta though. Which id? Do you have example code?

    Thanks again for your help. It’s getting there!
    -Panos

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #14763

    Hi Panos,
    Thank you.

    I was suggesting that when you created the gallery, you get gallery id. You can store it in the user meta for that user and retrieve it later to check for the existence of gallery etc.

    I am assuming that you are using the get_current_user_id() if hooking to mpp_action.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 12
    Panos on #14774

    I see what you mean. Got it. Thanks!

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #14780

    Great. Marking it as resolved.

  • Participant
    Level: Initiated
    Posts: 12
    Panos on #14791

    Brajesh,

    I didn’t want to create another post since this is the same subject but let me know if I should. Not a big deal but it would be great if it could be done. My question is, can I hook the auto-create Gallery function to the BP Autologin on Activation plugin? That way it just runs once and that’s that.

    Thanks for all you do!
    -Panos

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #14794

    I will recommend using ‘bp_first_activity_for_member’. That should do it.

    Regards
    Brajesh

The topic ‘ [Resolved] Help w/ auto-create gallery code’ is closed to new replies.

This topic is: resolved