Set featured on upload


#1

How can I set the featured flag during upload? I’m attempting to create an easier way for users to set their featured image which I’m using as a cover image for their profile page. Within the users profile settings, I have a new menu item called ‘Cover Image’. Within there I’ve put an uploader.

I think you guys are doing something somewhat similar with InspireBook. When the user clicks on “Change Cover” the uploaded image is automatically flagged as featured.


How to add 'Set featured' button?
#2

+1 For making it easier to let a user set a Cover Photo. Another option would be to show the option when a user hovers over a photo in an album… “Set this photo as Featured Photo”


#3

I just noticed on your InspireBook feature list that you state

Special “rtMedia” Features As this is made-for-rtMedia theme, it has 2 special features which cannot be delivered from rtMedia plugin alone. They are editing “Cover Photo” and editing “Profile Picture” as we are used to do it on Facebook.

To be clear, I’m not asking for all of the InspireBook code that allows you to change the cover photo. I’m only asking how to auto set featured on an upload.


#4

@bowefrankema You can do this if you have rtMedia Pro. I’ve done it. I’m hesitant to share the complete code because its Pro code, but basically do this.

In your functions.php file, first remove the edit and delete buttons

remove_action( 'rtmedia_before_item', 'add_action_buttons_before_media_thumbnail', 11 );

Then create your own action

add_action( 'rtmedia_before_item', 'add_action_buttons_before_media_thumbnail_custom', 11 );

Then copy the add_action_buttons_media_thumbnail function over and edit it to include a ‘set featured’ icon. You can easily do this by duplicating the Edit icon line and changing the end of the permalink to ‘featured’ instead of ‘edit’


#5

Thank you so much I’m indeed a Pro user. I’ll give it a try to implement myself :slight_smile:

Btw I think there’s no reason why you would not be able to share the snippet with the community. The more of these snippets available the more value the Pro version will have!

RE: The “Cover Photo” feature in the InspireBook theme: Of course it’s up to the RT Media team to keep some features exclusive to their theme. But it would certainly be a nice addition to the Pro version of the plugin. I actually thought that feature was part of the Pro version but misinterpreted the screenshots (you have the ability to set a picture as an album cover not a cover photo).

Either way I’m not complaining about the pro Features, but the more of these features are added the more likely it’ll become for those who use custom themes/3rd party themes to purchase the Pro version!


#6

If the rtCamp guys dont see any issue with it, I’ll be more than happy to.


#7

@rtCamp is this allowed?


#8

Hi @colabsadmin and @bowefrankema,

Yes it is allowed to share code snippet to help other users like we do on support thread unless it reveals the whole feature :wink:

Regarding setting featured media and use it like “Cover Photo”, check this line of code -> https://github.com/rtCamp/rtMedia/blob/master/app/main/controllers/media/RTMediaFeatured.php#L57 from where you can know how featured media gets set for user.


#9

Thanks! @colabsadmin could you share the snippet? I’m a very novice at writing php stuff so rather not reinvent the wheel :slight_smile:


#10

@riteshpatel

Yeah, I saw the function to set featured, my question was “is there away to auto set it on upload?” I’m using the uploader shortcode in the members profile settings. I was hoping there way a flag I could pass with the upload that would automatically set it as featured. If there’s no way to do that, is there a way I can run the set featured function automatically on a successful upload? Just looking for some advice on the best way to do this. Thanks!

@bowefrankenma Put this in your functions.php file.

/** Add 'Set/Unset Featured' to media overaly only if user is in their profile **/
if ( bp_is_my_profile() ) {

	remove_action( 'rtmedia_before_item', 'add_action_buttons_before_media_thumbnail', 11 );
	
	add_action( 'rtmedia_before_item', 'add_action_buttons_before_media_thumbnail_custom', 11 );
	
	function add_action_buttons_before_media_thumbnail_custom() {
	$featured_id = get_user_meta ( bp_loggedin_user_id(), "rtmedia_featured_media", true );	
		// add edit and delete links on single media
		global $rtmedia_media, $rtmedia_backbone;
		?>
		<?php
			if ( is_user_logged_in() ){
				if ( $rtmedia_backbone[ 'backbone' ] ){
					echo "<%= media_actions %>";
				} else {
					if( isset( $rtmedia_media ) && isset( $rtmedia_media->media_author ) && $rtmedia_media->media_author == get_current_user_id() ){
						?>
						
'> <?php _e( 'Edit', 'rtmedia-pro' ); ?> '> <?php _e( 'Delete', 'rtmedia-pro' ); ?> '> <?php _e( ( $featured_id == rtmedia_id() ? 'Unset' : 'Set') . ' Featured', 'rtmedia-pro' ); ?>
<?php } } } ?> <?php } }

#11

@colabsadmin,

There isn’t any such flag which you can pass and can set media as featured but you can use this hook rtmedia_after_add_media -> https://github.com/rtCamp/rtMedia/blob/master/app/main/controllers/media/RTMediaMedia.php#L174 which is called after media added and can set that media as featured. Of-course you need to send some parameters which you can check whether it is uploaded via shortcode or from somewhere else and can set featured media according to that parameter.


#12

Thank you. I’ll give that a go.


#13

@riteshpatel It took me awhile to sort it out, but I finally did. Thank you!


#14

Sorry to ask again but I’d love to do the same for my community. Any chance you could share the snippet again?


#15

I’m using it in a new profile settings screen that’s slug is ‘change-cover’. You’ll have to edit that part to suit your needs.

rtmedia_uploader();
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] );
	}
}

#16

Perfect! Thanks again for sharing. I’m planning to do a short write up with tips and tricks related to RTMedia soon on http://bp-tricks.com. I feel not enough people know about this plugin :slight_smile:


#17

No problem. And I agree, this plugin is one of the best I’ve used.


#18

Small update… I’m very close on getting everything to work… I’ve added the new screen which includes the media uploader… But the only thing that’s not happening is the image actually being set after upload.

Here’s my code: https://gist.github.com/BoweFrankema/9b298f9e64bb03b62f03

Any ideas what I’m doing wrong?


#19

Notice right after rtmedia_uploader(); there is a function ‘set_featured_after_upload’? I dont see where you’re calling that function.


#20

You need something like this. Sorry for not including it to begin with.

add_action( 'rtmedia_after_add_media',  'set_featured_after_upload'  );

edited to remove array