⬅ Voir tous les messages


Quick tip if you use the ActivityPub plugin on your WordPress site, and if you write in multiple languages on your site. Use the activitypub_post_locale filter to make sure your posts on the Fediverse are set to the locale matching the post on your site.

/**
 * Set a post's language in its ActivityPub representation
 * based on post tags.
 * The post tags must match an array of language codes you use on your site (in my example, English, Hungarian, and French).
 * When no tags are found, the post's language uses the default (the blog's language).
 *
 * @param string  $lang    The locale of the post.
 * @param int     $post_id The post ID.
 * @param WP_Post $post    The post object.
 *
 * @return string The filtered locale of the post.
 */
function jeherve_custom_ap_language( $lang, $post_id, $post ) {
	// Get the post's hashtags.
	$post_tags = get_the_tags( $post_id );

	if ( ! empty( $post_tags ) ) {
		// Is there a "en, "hu", or "fr" tag?
		foreach ( $post_tags as $tag ) {
			if ( in_array( $tag->slug, array( 'en', 'fr', 'hu' ), true ) ) {
				$lang = $tag->slug;
				break;
			}
		}
	}

	return $lang;
}
add_filter( 'activitypub_post_locale', 'jeherve_custom_ap_language', 10, 3 );

This way, I can add the tag to this post, and ensure the post will be shown as an English-language post for folks on the Fediverse.

This is important for folks who filter posts per language on the Fediverse, or who use translation tools to automatically translate posts in foreign languages.


  1. Avatar de Jeremy Herve

    If that code snippet doesn’t look too readable on your Fediverse client, be sure to visit the original post, that should help!


Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *