有効なXHTML 1.1を返すWordPressのlanguage_attributes関数を取得するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1805718

質問

次の要素を含むWordPressテンプレートがあります:

<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes('xhtml'); ?>>

これは以下を返します:

<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US" xml:lang="en-US">

残念ながら<!> quot; lang <!> quot;属性は無効なXHTML 1.1です-クライアントはこのレベルの検証を望んでいます。

WordPressのgeneral-template.phpファイルには次のコードが含まれています。

if ( get_option('html_type') == 'text/html' || $doctype == 'html' )
    $attributes[] = "lang=\"$lang\"";

$doctypeはそれに渡されるパラメーターです(この場合は 'xhtml')。 get_optionは「text / html」以外の値を返す必要がありますか?もしそうなら、これを達成するためにWordPressで何を設定する必要がありますか?

また、preg_replaceを使用して<!> quot; lang <!> quotを削除しようとしました。属性ですが、これはテキストと一致することができなかったようです。テキストを手動で入力すると、一致します!おそらく、language_attributesによって返される文字列のエンコーディングの問題ですか?

役に立ちましたか?

解決

これを解決しました。 <!> quot; language_attributes <!> quot;があります。フィルターなので、私はプラグインを作成し、それをフックして簡単なpreg_replaceを実行しました。ここで実行すると置換は機能し、それを処理するのに非常にきちんとした方法です。

編集

要求に応じて、使用したコードは次のとおりです。

<?php
/*
Plugin Name: Create Valid XHTML 1.1
Plugin URI: http://www.mycompany.com/create_valid_xhtml_1_1
Description: Removes deprecated "lang" attribute from (X)HTML header.
Author: dommer
Version: 1.0.0
Author URI: http://www.mycompany.com
*/

function create_valid_xhtml_1_1($language_attributes) 
{
    return preg_replace('/ lang=\"[a-z]+\-[A-Z]+\"/', '', $language_attributes);
}

add_filter('language_attributes', 'create_valid_xhtml_1_1');
?>

他のヒント

これが自分のサイトの単なるテーマである場合、header.phpを編集して変更することができます

<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes('xhtml'); ?>>

ハードコーディングする行、パフォーマンスも向上します:-)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top