代码之家  ›  专栏  ›  技术社区  ›  user2197774

向嵌入式WordPress嵌入式YouTube视频添加标题属性

  •  0
  • user2197774  · 技术社区  · 5 年前

    我正在尝试将标题属性添加到通过WordPress5编辑器嵌入的YouTube视频中。这是一个可访问性要求。

    function add_title_to_iframe_oembed( $html, $url, $attributes, $post_id ) {
    
        // Bail if this isn't an iframe
        if ( strpos( $html, '<iframe' ) === false ) {
            return $html;
        }
        // Bail if the attributes already contain a title
        if ( array_key_exists( 'title', $attributes ) ) {
            return $html;
        }
        // Define the title for the iframe, depending on the source content
        // List is based on supported Video and Audio providers at https://codex.wordpress.org/Embeds
        $url = parse_url( $url );
        $title = '';
        switch ( str_replace( 'www.', '', $url['host'] ) ) {
            /**
             * Video
             */
            case 'animoto.com':
            case 'blip.com':
            case 'collegehumor.com':
            case 'dailymotion.com':
            case 'funnyordie.com':
            case 'hulu.com':
            case 'ted.com':
            case 'videopress.com':
            case 'vimeo.com':
            case 'vine.com':
            case 'wordpress.tv':
            case 'youtube.com':
                $title = __( 'Video Player', 'n7studios' );
                break;
            /**
             * Audio
             */
            case 'mixcloud.com':
            case 'reverbnation.com':
            case 'soundcloud.com':
            case 'spotify.com':
                $title = __( 'Audio Player', 'n7studios' );
                break;
            /**
             * Handle any other URLs here, via further code
             */
            default:
                $title = apply_filters( 'add_title_to_iframe_oembed', $title, $url );
                break;
        }
        // Add title to iframe, depending on the oembed provider
        $html = str_replace( '></iframe>', ' title="' . $title . '"></iframe>', $html );
        // Return
    
        //var_dump($html); exit;
        return $html;
    }
    add_filter( 'embed_oembed_html', 'add_title_to_iframe_oembed', 10, 4 );
    

    我只能在var_转储HTML时看到标题。这就像是另一个过滤器正在删除标题。

    0 回复  |  直到 5 年前