Files
chrnxandClaude Opus 5 0a90e78783 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>
2026-08-14 19:05:55 -05:00

176 lines
5.7 KiB
PHP

<?php
/**
* Theme setup.
*
* @package CHRNX
*/
/**
* Register the theme's core WordPress features.
*/
function chrnx_setup() {
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'responsive-embeds' );
add_theme_support( 'editor-styles' );
add_editor_style( array( 'assets/css/base.css', 'assets/css/layout.css', 'assets/css/components.css' ) );
remove_action( 'wp_footer', 'the_block_template_skip_link' );
add_image_size( 'chrnx-card', 720, 480, true );
register_nav_menus(
array(
'chrnx-primary' => __( 'Primary navigation', 'chrnx' ),
'chrnx-footer' => __( 'Footer navigation', 'chrnx' ),
)
);
// No template renders a comment thread, so nothing is collected silently.
remove_post_type_support( 'post', 'comments' );
remove_post_type_support( 'page', 'comments' );
}
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.
*
* ponytail: an installed SEO plugin already emits these per post, with the
* right title and image. Two sets of og: tags let crawlers pick the wrong one,
* so the theme yields and only covers the no-plugin case.
*/
function chrnx_print_social_meta() {
if ( chrnx_has_seo_plugin() ) {
return;
}
if ( is_singular() ) {
$title = get_the_title();
$url = get_permalink();
$description = wp_strip_all_tags( get_the_excerpt() );
$type = 'article';
$image_id = get_post_thumbnail_id();
} else {
$title = get_bloginfo( 'name' );
$url = home_url( '/' );
$description = get_bloginfo( 'description' );
$type = 'website';
$image_id = 0;
}
// Generated derivative, not the design master: masters are not shipped.
$image = $image_id
? wp_get_attachment_image_url( $image_id, 'full' )
: get_theme_file_uri( 'assets/images/generated/about-grunge-1536.webp' );
$tags = array(
'og:site_name' => get_bloginfo( 'name' ),
'og:type' => $type,
'og:title' => $title,
'og:description' => $description,
'og:url' => $url,
'og:image' => $image,
);
foreach ( $tags as $property => $content ) {
if ( '' === $content ) {
continue;
}
printf(
'<meta property="%1$s" content="%2$s" />' . "\n",
esc_attr( $property ),
esc_attr( $content )
);
}
echo '<meta name="twitter:card" content="summary_large_image" />' . "\n";
}
add_action( 'wp_head', 'chrnx_print_social_meta' );
/**
* Report whether an SEO plugin already printed the document head.
*
* Deliberately not "is one installed": Rank Math skips its whole frontend
* until its setup wizard completes, so an installed-but-silent plugin used to
* make the theme yield to nobody. rank_math/head fires only when it really
* rendered, and it runs at wp_head priority 1, ahead of the tags below.
*
* @return bool True when an SEO plugin printed its head block.
*/
function chrnx_has_seo_plugin(): bool {
return did_action( 'rank_math/head' ) > 0;
}
/**
* Drop the relocated copy of core's title tag.
*
* Rank Math moves _wp_render_title_tag off wp_head and into its own
* rank_math/head block, but it does not know about the block-theme renderer
* that replaced it, so block themes render two identical <title> elements.
*
* Remove the relocated copy, never the block one. Both carry the same text,
* because Rank Math supplies it through pre_get_document_title, and keeping
* the block renderer means a page still has a title when the plugin is
* absent or silent. No guard is needed: with no Rank Math, rank_math/head
* holds no callbacks and this is a no-op.
*/
function chrnx_remove_duplicate_title_tag() {
remove_action( 'rank_math/head', '_wp_render_title_tag', 1 );
}
add_action( 'wp_head', 'chrnx_remove_duplicate_title_tag', 0 );