#WordPress のプラグイン #ActivityPub の #バグ を解消するため、テーマファイルエディターのfunctions.phpではなく、プラグインのコードを直接編集してあるので、ver.7.6.1 の段階での編集箇所をまとめて記載しておく。
まず、 #Mastodon で表示される日本語の #ハッシュタグ が「エンコード文字列から%が抜けた文字列」になってしまう #バグ への対処に関して。
/wp-content/plugins/activitypub/includes/functions.php
/**
* Escapes a Tag, to be used as a hashtag.
*
* @param string $input The string to escape.
*
* @return string The escaped hashtag.
*/
function esc_hashtag( $input ) {
$hashtag = \wp_specialchars_decode( $input, ENT_QUOTES );
// Remove all characters that are not letters, numbers, or hyphens.
$hashtag = \preg_replace( '/emoji-regex(*SKIP)(?!)|[^\p{L}\p{Nd}\-%]+/u', '-', $hashtag );
// Capitalize every letter that is preceded by a hyphen.
$hashtag = preg_replace_callback(
'/-+(.)/',
function ( $matches ) {
return strtoupper( $matches[1] );
},
$hashtag
);
// Add a hashtag to the beginning of the string.
$hashtag = ltrim( $hashtag, '#' );
$hashtag = trim( $hashtag, '-' );
$hashtag = '#' . $hashtag;
/**
* Allow defining your own custom hashtag generation rules.
*
* @param string $hashtag The hashtag to be returned.
* @param string $input The original string.
*/
$hashtag = apply_filters( 'activitypub_esc_hashtag', $hashtag, $input );
$hashtag = urldecode( $hashtag );
return esc_html( $hashtag );
}
修正行の元のコード:
$hashtag = \preg_replace( '/emoji-regex(*SKIP)(?!)|[^\p{L}\p{Nd}-]+/u', '-', $hashtag );
追加したコード:
$hashtag = urldecode( $hashtag );
英語の大文字が小文字になったり、ドイツ語のウムラウトなどが通常のアルファベットの小文字になってしまう問題(例: "ActivityPub"→"activitypub"、"Wälder"→"walder")の解消を期待した修正箇所。
/wp-content/plugins/activitypub/includes/transformer/class-post.php
/**
* Returns a list of Tags, used in the Post.
*
* This includes Hash-Tags and Mentions.
*
* @return array The list of Tags.
*/
protected function get_tag() {
$tags = parent::get_tag();
$post_tags = \get_the_tags( $this->item->ID );
if ( $post_tags ) {
foreach ( $post_tags as $post_tag ) {
// Tag can be empty.
if ( ! $post_tag ) {
continue;
}
$tags[] = array(
'type' => 'Hashtag',
'href' => \esc_url( \get_tag_link( $post_tag->term_id ) ),
'name' => esc_hashtag( $post_tag->name ),
);
}
}
return \array_unique( $tags, SORT_REGULAR );
}
修正行の元のコード:
'name' => esc_hashtag( $post_tag->slug ),
バグ例("テスト"→"E38386E382b9E38388")の画像:

wp_termsテーブル内の"name"と"slug"の例:

追加修正:
プラグイン #ActivityPub の設定「投稿コンテンツ」に指定した[ap_hashtags]に相当する関数の修正。
/wp-content/plugins/activitypub/includes/class-shortcodes.php
/**
* Generates output for the 'ap_hashtags' shortcode.
*
* @return string The post tags as hashtags.
*/
public static function hashtags() {
$item = self::get_item();
if ( ! $item ) {
return '';
}
$tags = \get_the_tags( $item->ID );
if ( ! $tags ) {
return '';
}
$hash_tags = array();
foreach ( $tags as $tag ) {
// Tag can be empty.
if ( ! $tag ) {
continue;
}
$hash_tags[] = \sprintf(
'<a rel="tag" class="hashtag u-tag u-category" href="%s">%s</a>',
\esc_url( \get_tag_link( $tag ) ),
esc_hashtag( $tag->name )
);
}
return \implode( ' ', $hash_tags );
}
修正行の元のコード:
esc_hashtag( $tag->slug )



コメント
この投稿のap_outboxの、タグに関するJSONは次の通りで、アルファベットが小文字になっていない。
#WordPress #ActivityPub #Mastodon
ap_outboxのJSONとは異なる表示をMastodonが行っている原因 #Gemini に尋ねたら、
「Mastodonはap_outboxのデータではない、別の場所を見ているわけではありません。 Mastodonが見ているのは、ap_outboxに保存されたJSONをテンプレートとして利用し、送信直前にプラグインが上書き・変換した最終的なペイロードです。」とのこと。
「つまり、ap_outboxのJSONデータは、プラグインが内部的にタグ情報として保存しているだけで、最終的な送信データではタグのスラッグ情報などを使って、よりFediverse標準に合わせた形に上書きされている、ということです。」とのこと。
別のコードも変えないといけないのか…。
#ActivityPub #WordPress
"この投稿のap_outboxの、タグに関するJSONは次の通りで、アルファベットが小文字になっていない。
"
プラグインActivityPub(7.6.1)の私的修正箇所 | いしい@試行錯誤
https://ishii00141.stars.ne.jp/20251125-0039-4145/#comment-1161
それにもかかわらず、#Mastodon で小文字になってしまう理由を、コードを示しながら #Gemini に調べてもらったけれど、結局は ap_outbox の生成コードに戻ってしまって、くるくるくる繰り返すだけで、疲れた。
ちなみに、v4.1.25 のmstdn.jpでも小文字化しているので、 #Mastodon の仕様変更とは考えにくい。
mastodon.social でも、プラグインActivityPub(7.6.0)になる前は大文字が維持されていて、JSONのコードも、上記と同じ構造だった。
この記事の
#Mastodon (mstdn.jp、v4.1.25)で表示されたハッシュタグ:
以下は #WordPress のプラグイン #ActivityPub が7.6.0になる前の例。
outlook.comのブックマークからアクセスできない | いしい@試行錯誤
https://ishii00141.stars.ne.jp/20251111-1858-3873/
#Mastodon (mstdn.jp、v4.1.25)で表示されたハッシュタグ:
JSONのタグ部分:
見つけた。追加して修正すべき箇所。
#Mastodon に送信するデータを指定する設定にあるショートコード[ap_hashtags]。
この関数のコードを修正する必要がある。
/wp-content/plugins/activitypub/includes/class-shortcodes.php
修正前のコード
このコードの修正だけで十分だったかもしれない。
esc_hashtag()関数に送られるデータが"$tag->name"なので、私の場合は"%"が含まれていないはず(タグに"%"は使わない)だから、置換コードで"%"を除外する必要はないし、get_tag()関数の方は、ショートコード[ap_hashtags]とは関係なさそうなので、無視しても良さそう。
ハッシュタグが全て小文字になってしまったことで、読み上げソフトで誤って発音される問題が生じたとのこと。
"Since a few releases, there seems that tag handling has changed. It now converts all uppercase characters to lowercase, even if not necessary. This makes some tags harder readable as well as less accessible, since they will be pronounced differently by screen readers.
Steps to reproduce
1. Add a new post.
2. Add tags CalDAV, macOS, self-hosting, iPhone
Actual output of the tags: #caldav #macos #selfHosting #iphone
Expected output of the tags: #CalDAV #macOS #selfHosting #iPhone
It should only strip out special characters, spaces and such and make the next character uppercase without touching any existing uppercase character.
"
Case in tags is not preserved · Issue #2562 · Automattic/wordpress-activitypub
https://github.com/Automattic/wordpress-activitypub/issues/2562
includes/class-shortcodes.php
includes/model/class-blog.php
includes/rest/class-collections-controller.php
includes/transformer/class-post.php
で、
「esc_hashtag( $tag->name )」に変更されるらしい。
"Proposed changes:
・Use tag->name instead of tag->slug for hashtag display
・Add test cases for tag names with capitals and underscores
"
Use tag name instead of slug for hashtag display by pfefferle · Pull Request #2564 · Automattic/wordpress-activitypub · GitHub
https://github.com/Automattic/wordpress-activitypub/pull/2564
この記事の修正は、プラグイン「ActivityPub」が7.7.0に更新されたことで上書きされた。
プラグイン「ActivityPub」が7.7.0に更新されたことで、この記事のような修正が必要だった問題は解消された。