Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kasparsd/edb772c864ea6baa567f to your computer and use it in GitHub Desktop.
Save kasparsd/edb772c864ea6baa567f to your computer and use it in GitHub Desktop.
<?php
add_filter( 'cf7_entry_rows', function( $rows, $entry ) {
$attachments = get_post_meta( $entry->ID, 'mail_attachments', true );
if ( empty( $attachments ) ) {
return $rows;
}
$uploads_dir = wp_upload_dir();
$storage_url = sprintf( '%s/cf7-storage', $uploads_dir['baseurl'] );
$attachment_list = array();
foreach ( $attachments as $path ) {
$extension = pathinfo( $path, PATHINFO_EXTENSION );
$link = sprintf( '%s/%d-%s.%s', $storage_url, $entry->ID, md5( $path ), $extension );
// Render as a preview
if ( in_array( $extension, array( 'jpeg', 'jpg', 'png', 'gif' ) ) ) {
$preview = sprintf(
'<img src="%s" alt="" width="300" />',
$link
);
} else {
$preview = esc_html( basename( $path ) );
}
$attachment_list[] = sprintf(
'<li><a href="%s" target="_blank">%s</a></li>',
esc_url( $link ),
$preview
);
}
$rows['attachments']['value'] = sprintf(
'<ul>%s</ul>',
implode( '', $attachment_list )
);
return $rows;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment