This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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