This is theme dependent, but with most themes you can use the “language_attributes” filter to return whatever attribute string you want as the html tag’s attributes.
The filter callback you add would need to somehow know which pages are English. I don’t know how you’d determine that, but I can tell you the queried object’s ID can be determined with get_queried_object_id(). If 0 is returned, it’s not a single page request. The returned object ID can possibly be any single WP object, but it’s generally a WP_Post object.
You can utilize the page or post slug to determine the English pages. I am presenting a sample code block below that can be followed to replace the existing lang attribute with a new one. Please note that this process requires proficiency in coding.
/**
* Modify language attributes for specific pages and posts.
*
* @param string $output HTML attributes for the <html> tag.
* @return string Modified HTML attributes for the <html> tag.
*/
function my_custom_language_attributes( $output ) {
// Define an array of page and post slugs to modify.
$page_slugs = array( 'my-page-slug', 'my-post-slug' ); // Add any additional page or post slugs here
// Check if the current page or post is in the defined array of slugs.
if ( is_page( $page_slugs ) || is_single( $page_slugs ) ) {
// Get the current language.
$lang = get_bloginfo('language');
// Set the desired language.
$new_lang = 'en-US';
// Replace the current language with the new language in the HTML attributes.
$output = str_replace('lang="' . $lang . '"', 'lang="' . $new_lang . '"', $output);
}
// Return the modified HTML attributes.
return $output;
}
// Hook the function to the language_attributes filter.
add_filter( 'language_attributes', 'my_custom_language_attributes' );
Thank you very much @aktarzaman , it is exactly what I needed and it did work.
@jj7374107 I had the same challenge and wrote a lightweight plugin to do this more conveniently from the Posts edit screen:
https://swissmacuser.ch/wordpress-html-lang-locale-overwrite-plugin/
@jj7374107 where put this code, how did you do this