Skip to content

Instantly share code, notes, and snippets.

@yuriinalivaiko
Last active November 11, 2023 15:50
Show Gist options
  • Save yuriinalivaiko/8cd1cf313bba5f8d9bfabeda8d25647e to your computer and use it in GitHub Desktop.
Save yuriinalivaiko/8cd1cf313bba5f8d9bfabeda8d25647e to your computer and use it in GitHub Desktop.
This code snippet extends the "File upload" field type to add video formats. An array of supported video formats: 'mp4', 'm4v', 'webm', 'ogv', 'flv'.
<?php
/**
* Allow to upload video files via the "File Upload" field type.
* Add this code to the file functions.php in the active theme directory.
*/
add_filter( 'um_allowed_file_types', 'um_add_video_file_types' );
add_filter( 'um_profile_field_filter_hook__file', 'um_profile_field_filter_hook__file_video', 101, 2 );
/**
* Extend "Allowed File Types" in the File Upload field.
*
* @param array $allowed_types List of supported formats.
* @return array
*/
function um_add_video_file_types( $allowed_types ) {
$video_file_types = array();
foreach( wp_get_video_extensions() as $ext ) {
$video_file_types[ $ext ] = strtoupper( $ext ) . ' video';
}
return array_merge( $video_file_types, (array) $allowed_types );
}
/**
* Embed video files and play them in profile view.
*
* @param string $value Field Value.
* @param array $data Field Data.
* @return string
*/
function um_profile_field_filter_hook__file_video( $value, $data ) {
$filename = um_user( $data['metakey'] );
$fileinfo = um_user( $data['metakey'] . '_metadata' );
if ( $filename && is_array( $fileinfo ) && in_array( $fileinfo['ext'], wp_get_video_extensions(), true ) ) {
$file_url = trailingslashit( UM()->uploader()->get_upload_user_base_url( um_profile_id() ) ) . $filename;
$shortcode = '[video src="' . esc_url( $file_url ) . '"]';
$value = apply_shortcodes( $shortcode );
}
return $value;
}
@yuriinalivaiko
Copy link
Author

yuriinalivaiko commented Oct 28, 2022

This gist is a part of the article um_allowed_file_types.

Documentation: https://docs.ultimatemember.com/
Support forum: https://wordpress.org/support/plugin/ultimate-member/

Related WordPress documentation:

Examples

Image - The File Upload field type with allowed video formats.
UM Forms, Edit Form, Edit Field - File Upload (video)

Image - The File Upload field type with video in profile.
UM Profile + Video fields (Video Upload)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment