Edit Custom Fields for Attachments


Hi, I have been able to include Custom Fields to Attachments, BUT I can't figure out how to include them on the "edit image" screens. I see that we're including title [$bp_media_current_entry->get_title();], description [$bp_media_current_entry->get_content();] & now the "bp_media_add_media_fields".. but how can I include Custom Fields?

I understand I'll need to hack a few files.. that's ok with me if it's what's necessary.




Hi, You don't need to make any changes to BuddyPress Media files for this. You can include custom fields to attachments by using the bp_media_add_media_fields action.

You can add the following piece of code to your themes functions.php

function custom_add_media_edit_fields($media_type){  
    //$media_type gives you the type of media (image, video, audio) for conditional checks  
    global $bp_media_current_entry;  
    $media_id = $bp_media_current_entry->get_id();  
    $custom_field = get_post_meta($media_id, '_my_custom_field', true); ?>  
    My Custom Field  

Then you would need to use the bp_media_after_update_media to store the updated value

add_action('bp_media_after_update_media', 'save_my_custom_field');  
function save_my_custom_field($object){  
    $type = $object->get_type(); // gives you the media type for conditional checks  
    $id = $object->get_id();  
    $my_custom_field = $_POST['my-custom-field'];  

That should do the trick.


I'm used the plugin advanced custom fields, How I can make for included my code inputs fields in before upload of media

with this code I can make it? or this is only for edit field media?



@Joshua.. thanks so much! This is great!

Do you know how I can now display the data from this custom field on my BuddyPress-Media item? I see the field when I click 'edit', but don't see it when I click the media & see the title & description data.

Much appreciated!


@Leandro Junqueira - The above code is for the edit media fields only

@kennypetrowski - At present there are no proper hooks to display them directly. But you could hook them onto bp_media_before_content or bp_media_after_content. But they won't show up if your lightbox is enabled. We are in the process of refactoring BuddyPress Media to make it more flexible so that it can incorporate such things easily.


Ok @ Joshua

Other question, there is how put custom field before upload.

I await, Thanks!


@Leandro - Right now there is no direct method of doing this. We are in the process of refactoring the code for more flexibility. Once the refactoring is complete it will be easier to handle such things. You can check the roadmap here to get an idea of the upcoming releases.



How do I show the custom field next to the description?



You would need to override the media/media-single-edit.php and paste your code after the "rtmedia-editor-description" div. For more information on overriding templates in rtMedia check this link -> http://rtcamp.com/rtmedia/docs/developer/templating-system/