Set featured on upload

Thanks again for your help @colabsadmin. I think I’m getting closer but I’m quite new to learning how to use functions/actions properly :slight_smile:

I thought I had it working (the Featured image was actually set) with the following code:

//Set uploaded media as cover photo after upload
function set_featured_after_upload ( $media_id, $file_object, $uploaded ) {
	global $bp;
	update_user_meta ( bp_loggedin_user_id(), 'rtmedia_featured_media', $media_id[0] );
}
add_action( 'rtmedia_after_add_media',  'set_featured_after_upload'  );

// Show the tab content
function bp_custom_screen_content() {
	rtmedia_uploader();
	set_featured_after_upload();
}

That seemed to worked (besides showing some warnings) but as soon as I would move to another page the Featured image would be unset again.

So I figured you did not put the if_current_action check for nothing… so I added that back. As soon as I did that nothing happens after upload. I do get the following errors on both occasions:

Warning: Missing argument 1 for set_featured_after_upload(), called in custom.php on line 187 and defined in custom.php on line 176

I’ve updated my broken code in the gist…

I’ll now try and figure out if maybe there is a different $bp->current_action I need to use… Seems like the problem lies somewhere there :ok_woman:

Remove your set_featured_after_upload(); call in bp_custom_screen_content. The add_action is handling it for you and will only run set_featured_after_upload when something is uploaded via the change cover screen. The way it stands now it running every page load and setting the featured to null because it has nothing to set. Does that make sense?

Its hard to share my code because I have it wrapped in a class that does multiple things. But here is the skeleton

if ( !is_admin() ) {
     add_action( 'bp_xprofile_setup_nav', array( $this, 'setup_cover_profile_nav' ) );
     add_action( 'rtmedia_after_add_media', array( $this, 'set_featured_after_upload' ) );
}

function setup_cover_profile_nav(){
	global $bp;
	$profile_link = bp_loggedin_user_domain() . $bp->profile->slug . '/';
	bp_core_new_subnav_item( 
			array( 
				'name' => 'Profile Cover',
				'slug' => 'change-cover',
				'parent_url' => $profile_link,
				'parent_slug' => $bp->profile->slug, 
				'screen_function' =>array( $this, 'screen_change_cover' ),
				'user_has_access'   => ( bp_is_my_profile() || is_super_admin() ),
				'position' => 40 ) );
	 
	}
	
	function screen_change_cover(){
		global $bp;
		add_action( 'bp_template_title', array( $this,'page_title' ));
		add_action( 'bp_template_content', array( $this, 'page_content') );
		bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
	}
	function page_title(){
			echo 'Add/Update Your Profile Cover Image';
	}
	
	function page_content(){
			
            
<?php rtmedia_uploader(); ?>
**Note: The above uploader will allow you to upload multiple images. If you select multiple images, a random image will be set as your profile cover. <?php } function set_featured_after_upload ( $media_id, $file_object, $uploaded ) { global $bp; if ( $bp->current_action == 'change-cover' ) { update_user_meta ( bp_loggedin_user_id(), 'rtmedia_featured_media', $media_id[0] ); } }

No worries I’ll figure this out… I need to stop being lazy and dive into the proper way of doing things myself… I’ll get there :slight_smile:

Awesome :thumbsup: I got it working by modifying your code and moving some stuff around. I’m fairly certain there was a different do_action I needed to check for in the set_featured_after_upload function. Sadly I could not figure out what it was. Anyways here’s the final code I used and is working:

Can be added to any functions file (bp-custom.php/functions.php).

Yeah. I mistakenly forgot to remove the ‘array’ stuff in the do action. That array is needed when you’re using a class.

1 Like