Come posso ottenere la funzione language_attributes di WordPress per restituire XHTML 1.1 valido?
-
05-07-2019 - |
Domanda
Ho un modello WordPress che contiene il seguente elemento:
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes('xhtml'); ?>>
Questo restituisce:
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US" xml:lang="en-US">
Sfortunatamente il " lang " l'attributo non è valido XHTML 1.1 - e il client vorrebbe questo livello di convalida.
Il file general-template.php di WordPress contiene il seguente codice:
if ( get_option('html_type') == 'text/html' || $doctype == 'html' )
$attributes[] = "lang=\"$lang\"";
$doctype
è il parametro passato ad esso (in questo caso 'xhtml'). get_option
dovrebbe restituire un valore diverso da "text / html"? In tal caso, cosa devo impostare in WordPress per raggiungere questo obiettivo, se non altro?
Ho anche provato a usare preg_replace per eliminare " lang " attributo, ma questo non sembra essere in grado di abbinare il testo. Se inserisco il testo manualmente, corrisponde! Forse un problema di codifica con la stringa restituita da language_attributes?
Soluzione
Ho risolto questo. C'è un & Quot; language_attributes & Quot; filtro, quindi ho scritto un plug-in che si aggancia a quello e fa un semplice preg_replace. Il sostituto ha funzionato quando eseguito qui, ed è un modo abbastanza accurato per gestirlo.
Modifica
Come richiesto, ecco il codice che ho usato:
<?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');
?>
Altri suggerimenti
Se questo è solo un tema sul tuo sito, puoi modificare header.php e cambiare
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes('xhtml'); ?>>
linea da codificare, migliora anche le prestazioni :-)