wp_rel_callback()


WordPressのwp_rel_callback()関数は、HTML要素のrel属性をフィルタリングおよび修正するためのコールバック関数です。主にリンク関係(rel)属性の操作に使用されます。

構文

wp_rel_callback( string|array $matches );
  • $matches (string|array) — rel属性のマッチング結果。正規表現のキャプチャグループを含む。

例1: rel属性にnofollowを追加

リンクのrel属性にnofollowを追加する例。

add_filter('wp_rel_callback', function($matches) { return $matches[1] . ' nofollow'; });

例2: rel属性からexternalを削除

rel属性からexternalを削除するフィルタリング例。

add_filter('wp_rel_callback', function($matches) { return str_replace('external', '', $matches[1]); });

例3: 特定のURLにのみrel属性を追加

example.comへのリンクにのみnofollowを適用。

add_filter('wp_rel_callback', function($matches) { return (strpos($matches[2], 'example.com') !== false ? $matches[1] . ' nofollow' : $matches[1]; });

例4: rel属性を完全に上書き

全てのrel属性をnofollowに置き換える。

add_filter('wp_rel_callback', function($matches) { return 'nofollow'; });

例5: 複数のrel属性を管理

nofollowとnoopenerを同時に追加。

add_filter('wp_rel_callback', function($matches) { return $matches[1] . ' nofollow noopener'; });

例6: 空のrel属性を処理

rel属性が空の場合にデフォルト値を設定。

add_filter('wp_rel_callback', function($matches) { return empty($matches[1]) ? 'nofollow' : $matches[1]; });

例7: 特定のクラスを持つ要素のみ処理

external-linkクラスを持つ要素のみにrel属性を追加。

add_filter('wp_rel_callback', function($matches) { return strpos($matches[3], 'external-link') !== false ? $matches[1] . ' external' : $matches[1]; });

注意事項

  • この関数は直接呼び出すのではなく、フィルターフックとして使用します。
  • 正規表現のマッチング結果に依存するため、入力形式に注意が必要です。
  • 複数のフィルターが適用される場合、優先順位に気を付けてください。

関連機能: