@ Joshua
RE: Is it hard to associate an existing media library item (or image url) with a business connections entry programmatically?
Sorry, but this will not be possible. Connections uses its own image “stash” for the lack of a better term. This is primarily for performance/usability reasons. WordPress stores all its image meta as a post type in the database. So when you access an image, you have to first hit the DB, then the image. Connections can also crop/scale on demand and cache’s them. With the Media Library, a new image size needs registered, then a plugin like Regenerate thumbnails needs to be run to create the new size for all images.
The best that could be done is “sideload” an image from the Media Library into the Connections cache.
Here’s a code snippet I have handy for sideloading an image added to an Connections Entry into the Media Library. This does not link the two images in any way. It just adds the image to the Media Library.
add_action( 'cn_post_process_add-entry', 'cn_insert_image_into_media_library' );
add_action( 'cn_post_process_update-entry', 'cn_insert_image_into_media_library' );
function cn_insert_image_into_media_library( $entry ) {
$image = $entry->getimageMeta();
if ( ! is_wp_error( $image ) ) {
// $file = array(
// 'url' => $image['url'],
// 'type' => $image['type'],
// 'path' => $image['path'],
// );
// $time = current_time( 'mysql' );
// $url = $file['url'];
// $type = $file['type'];
// $file = $file['path'];
// $title = preg_replace('/\.[^.]+$/', '', basename( $file ) );
// $content = '';
// // Use image exif/iptc data for title and caption defaults if possible.
// if ( $image_meta = @wp_read_image_metadata( $file ) ) {
// if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
// $title = $image_meta['title'];
// }
// if ( trim( $image_meta['caption'] ) ) {
// $content = $image_meta['caption'];
// }
// }
// // Construct the attachment array.
// $attachment = array_merge(
// array(
// 'post_mime_type' => $type,
// 'guid' => $url,
// 'post_parent' => 0,
// 'post_title' => $title,
// 'post_content' => $content,
// ),
// array()
// );
// // This should never be set as it would then overwrite an existing attachment.
// unset( $attachment['ID'] );
// // Save the attachment metadata
// $id = wp_insert_attachment( $attachment, $file, 0 );
// if ( ! is_wp_error( $id ) ) {
// wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
// }
/*
* Read the contents of the upload directory. We need the
* path to copy the file and the URL for uploading the file.
*/
$uploads = wp_upload_dir();
$uploads_dir = $uploads['path'];
$uploads_url = $uploads['url'];
// Copy the file from the root directory to the uploads directory
copy( $image['path'], trailingslashit( $uploads_dir ) . $image['name'] );
/*
* Get the URL to the file and grab the file and load
* it into WordPress (and the Media Library)
*/
$url = trailingslashit( $uploads_url ) . $image['name'];
$result = media_sideload_image( $url, 0, $image['name'] );
// If there's an error, then we'll write it to the error log.
if ( is_wp_error( $result ) ) {
error_log( print_r( $result, true ) );
}
}
}
I do not have any code handy to sideload an image into an Entry from the Media Library. I do plan on writing a series on helper functions to make adding an logo/photo easy. I need these for the REST API for Connections I’ve been working on. But I’m not there yet.
You should be able to cobble something together from these lines of code:
And the code from this function:
You would need to tweak the to use an the absolute file path of an image from the Media Library instead of the files upload path.
Hope this helps!
