Skip to content

Instantly share code, notes, and snippets.

@janboddez
Last active March 22, 2023 08:07
Show Gist options
  • Save janboddez/5c6a69ca603d42b53904f7af6b2ad030 to your computer and use it in GitHub Desktop.
Save janboddez/5c6a69ca603d42b53904f7af6b2ad030 to your computer and use it in GitHub Desktop.
<?php
/**
* Adds video to Share on Mastodon's attachment array by hooking into Share on
* Mastodon's `share_on_mastodon_media` filter.
*
* Just drop this file into `wp-content/mu-plugins`.
*
* @param array $media Array of up to 4 image IDs.
* @param WP_Post $post Post object.
* @return array Modified array.
*/
add_filter( 'share_on_mastodon_media', function( $media, $post ) {
// @link https://wordpress.org/documentation/article/use-image-and-file-attachments/#attachment-to-a-post
$video = get_attached_media( 'video', $post->ID );
if ( empty( $video ) ) {
// Current post doesn't have any video attached to it. Return `$media` unaltered.
return $media;
}
// Grab the **first** video attachement.
$video = reset( $video );
// Return up to three images plus our one video file.
return array_merge( array_slice( $media, 0, 3 ), array( $video->ID ) );
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment