wp_filter_oembed_iframe_title_attribute()


WordPressの関数wp_filter_oembed_iframe_title_attribute()は、oEmbedのiframe要素にtitle属性を追加するために使用されます。

構文

wp_filter_oembed_iframe_title_attribute( string $return, string $url, array $attr );

引数の説明:

  • $return (string) — iframeのHTMLコード
  • $url (string) — 埋め込みコンテンツのURL
  • $attr (array) — 追加属性の配列

例1: 基本的な使用法

oEmbedのiframeにtitle属性を追加します。

add_filter('oembed_result','wp_filter_oembed_iframe_title_attribute',10,3);

例2: カスタムタイトルを設定

iframeのtitle属性をカスタマイズします。

function custom_oembed_title($html,$url,$attr){return str_replace('title="','title="カスタムタイトル ',$html);}add_filter('oembed_result','custom_oembed_title',10,3);

例3: 特定のURLのみに適用

YouTubeの埋め込みのみにtitle属性を追加します。

function youtube_oembed_title($html,$url,$attr){if(strpos($url,'youtube.com')!==false){return wp_filter_oembed_iframe_title_attribute($html,$url,$attr);}return $html;}add_filter('oembed_result','youtube_oembed_title',10,3);

例4: 属性を追加して拡張

title属性に加えて他の属性も追加します。

function enhanced_oembed_attributes($html,$url,$attr){$html=wp_filter_oembed_iframe_title_attribute($html,$url,$attr);return str_replace('<iframe ','<iframe data-embed="true" ',$html);}add_filter('oembed_result','enhanced_oembed_attributes',10,3);

例5: フィルターの優先度変更

フィルターの優先度を変更して最後に実行します。

add_filter('oembed_result','wp_filter_oembed_iframe_title_attribute',99,3);

例6: 複数フィルターの組み合わせ

複数のフィルターを組み合わせて使用します。

function multi_oembed_filters($html,$url,$attr){$html=wp_filter_oembed_iframe_title_attribute($html,$url,$attr);return apply_filters('my_custom_filter',$html);}add_filter('oembed_result','multi_oembed_filters',10,3);

例7: 条件付きタイトル追加

特定の条件でtitle属性を追加します。

function conditional_oembed_title($html,$url,$attr){if(!is_admin()){return wp_filter_oembed_iframe_title_attribute($html,$url,$attr);}return $html;}add_filter('oembed_result','conditional_oembed_title',10,3);

注意点

  • この関数はフィルターフック経由で使用します
  • title属性はアクセシビリティ向上に役立ちます
  • HTMLのバリデーションに影響する可能性があります

関連機能: