Create new album with photos in it with PHP


#1

Hi, I would need to create an album from my custom plugin.

I have all the media attachment ID’s, and from what I saw in db, albums are just a custom post type with bunch of related info saved in rt_rtm_media table.

Is there an easy way to do this? What classes should I study?

Thanks for help Vasik


#2

Ok, I found out that creating an album is easy:

$album = new RTMediaAlbum();
$rtmedia_id = $album->add( 'Title', get_current_user_id(), true, false, 'profile', 7 );

Now, how can I add photos to that album?..:slight_smile:

Thanks V.


#3

Ok, for anyone needing this, adding images is also super easy:

$ids = array();

// set featured image
if ( $sanitized_values['_nevestam_thumbnail']) {
    set_post_thumbnail( $post_data['ID'], $sanitized_values['_nevestam_thumbnail_id']);
    $ids[] =  $sanitized_values['_nevestam_thumbnail_id'];
}

$media = new RTMediaMedia();


$uploaded['album_id'] = $rtmedia_id;
$uploaded['context'] = 'profile';
$uploaded['context_id'] = 1;
$uploaded['media_type'] = 'image';
$uploaded['privacy'] = NULL;

foreach ($sanitized_values['_nevestam_photo_blog_gallery'] as $key => $url) {
    $ids[] = $key;

}

$media->insertmedia($ids, $uploaded, $uploaded);

#4

Hello @Vaclav_Greif,

Glad to know you figured out the solution. Thank you for your kind contribution at community support.

Thank you.


#5

No problem, glad to help, hope this will get some use…:slight_smile:


#6