Skip to content

Instantly share code, notes, and snippets.

@janboddez
Last active October 15, 2023 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janboddez/6851197fa3b2a416fb64070098e7526f to your computer and use it in GitHub Desktop.
Save janboddez/6851197fa3b2a416fb64070098e7526f to your computer and use it in GitHub Desktop.
<?php
// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_filter( 'import_from_mastodon_post_content', function( $content, $status ) {
// The plugin, by default, leaves hyperlinks in. This removes them.
return wp_kses(
$content,
array(
'br' => array(),
'p' => array(),
)
);
}, 10, 2 );
add_action( 'import_from_mastodon_after_attachments', function( $post_id, $status ) {
// Unset post thumbnail.
delete_post_thumbnail( $post_id );
// Get all uploaded images.
$attachments = get_attached_media( 'image', $post_id );
if ( ! empty( $attachments ) ) {
$post = get_post( $post_id );
$content = $post->post_content;
// Update post content. Classic editor only!
foreach ( $attachments as $attachment ) {
$content .= "\n\n" . wp_get_attachment_image( $attachment->ID, 'large' );
}
wp_update_post( wp_slash( array(
'ID' => $post_id,
'post_content' => $content,
) ) );
}
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment