Skip to content

Commit

Permalink
Filter page titles to enable translations
Browse files Browse the repository at this point in the history
Fixes #436
  • Loading branch information
ryelle committed Jun 11, 2024
1 parent 693c3cf commit cf88ba4
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion source/wp-content/themes/wporg-main-2022/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
add_filter( 'render_block_core/site-title', __NAMESPACE__ . '\use_parent_page_title', 10, 3 );
add_filter( 'render_block_data', __NAMESPACE__ . '\update_header_template_part_class' );
add_filter( 'wporg_block_navigation_menus', __NAMESPACE__ . '\add_site_navigation_menus' );
add_filter( 'the_title', __NAMESPACE__ . '\translate_the_title', 1, 2 );
add_filter( 'single_post_title', __NAMESPACE__ . '\translate_the_title', 1, 2 );

/**
* Enqueue scripts and styles.
Expand Down Expand Up @@ -274,7 +276,7 @@ function use_parent_page_title( $block_content, $block, $instance ) {
// Loop up to the first child page, this is the section title.
while ( $parent ) {
$url = get_permalink( $parent->ID );
$title = $parent->post_title;
$title = get_the_title( $parent );
$parent = get_post_parent( $parent );
}

Expand Down Expand Up @@ -319,6 +321,27 @@ function update_header_template_part_class( $block ) {
return $block;
}

/**
* Replace the title with the translated page title.
*
* @param string $title The current title, ignored.
* @param int $post_id The post_id of the page.
*
* @return string
*/
function translate_the_title( $title, $post_id = null ) {
if ( is_admin() ) {
return $title;
}

$post = get_post( $post_id );
if ( $post && 'page' === $post->post_type ) {
$title = translate( $post->post_title, 'wporg' ); // phpcs:ignore
}

return $title;
}

/**
* Prevent easy access to the Site Editor.
*
Expand Down

0 comments on commit cf88ba4

Please sign in to comment.