fix: declare the language each post is actually written in

Half the journal is Spanish, but every route declared en-US, so screen
readers pronounced Spanish posts with English phonetics and crawlers
indexed them as English. The chrnx_language taxonomy already recorded the
answer per post and nothing read it.

Map its terms onto the html lang attribute for single posts and for the
language archives, replacing only the language so dir and any plugin
attribute survive. Posts with no language term keep the site default.

Also stop advertising a comments feed. Comments are unsupported theme-wide,
yet the head still linked one, because feed_links_extra also fires for old
posts carrying a non-zero comment_count. The core filter covers both that
and the site-wide link while leaving topic, author and search feeds alone.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
chrnxandClaude Opus 5 committed 2026-08-14 19:05:55 -05:00
1 parent a4c68e3c56
commit 0a90e78783
2 files changed
+133

No files matched your search

+56
View File
@@ -32,6 +32,62 @@ function chrnx_setup() {
add_action( 'after_setup_theme', 'chrnx_setup' );
add_filter( 'comments_open', '__return_false' );
/*
* Nothing collects comments, so the head must not advertise a feed for them.
* This one filter covers both the site-wide feed printed by feed_links() and
* the per-post one from feed_links_extra(), which still fired on old posts
* carrying a non-zero comment_count. Topic, author and search feeds stay.
*/
add_filter( 'feed_links_show_comments_feed', '__return_false' );
/**
* Resolve the document language from the editorial language taxonomy.
*
* Roughly half the journal is written in Spanish while every route declared
* en-US, so screen readers read Spanish with English phonetics and crawlers
* indexed those posts as English. chrnx_language already records the answer.
*
* @return string BCP 47 tag, or an empty string to keep the site language.
*/
function chrnx_get_document_locale(): string {
// ponytail: explicit map, a third language is one more line here.
$locales = array(
'en' => 'en-US',
'es' => 'es-MX',
);
$slug = '';
if ( is_singular( 'post' ) ) {
// A post carries one language; extra terms are an editing mistake.
$terms = get_the_terms( get_queried_object_id(), 'chrnx_language' );
$slug = ( is_array( $terms ) && isset( $terms[0]->slug ) ) ? $terms[0]->slug : '';
} elseif ( is_tax( 'chrnx_language' ) ) {
$term = get_queried_object();
$slug = $term instanceof WP_Term ? $term->slug : '';
}
return $locales[ $slug ] ?? '';
}
/**
* Apply the editorial language to the html element.
*
* @param string $output Attribute string built by core.
* @return string Attribute string carrying the editorial language.
*/
function chrnx_filter_language_attributes( $output ) {
$locale = chrnx_get_document_locale();
if ( '' === $locale ) {
return $output;
}
// Replace only the language, so dir and any plugin attribute survive.
return preg_replace( '/lang="[^"]*"/', 'lang="' . esc_attr( $locale ) . '"', (string) $output, 1 );
}
add_filter( 'language_attributes', 'chrnx_filter_language_attributes' );
/**
* Print Open Graph and Twitter card tags only when nothing else does.
*