Skip to content

Commit

Permalink
Posts: Add "By" to the author name block in a translation-friendly way
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle committed Jan 23, 2024
1 parent b6ca464 commit e702884
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
add_action( 'parse_query', __NAMESPACE__ . '\compat_workaround_core_55100' );
add_filter( 'render_block_core/pattern', __NAMESPACE__ . '\convert_inline_style_to_rtl', 20 );
add_filter( 'render_block_core/query-title', __NAMESPACE__ . '\render_index_title', 10, 2 );
add_filter( 'render_block_core/post-author-name', __NAMESPACE__ . '\render_author_prefix', 10, 2 );

/**
* Blank out the archive title prefix sometimes.
Expand Down Expand Up @@ -140,3 +141,18 @@ function render_index_title( $content, $block ) {
}
return $content;
}

/**
* Add the "by" string to the post author block in an i18n-friendly way.
*
* @param string $content Content of the current block.
* @param WP_Block $block
* @return string The updated content.
*/
function render_author_prefix( $content, $block ) {
if ( preg_match( '#(<div[^>]*>)(.*)(</div>)#', $content, $matches ) ) {
// translators: %s is the author name.
$content = $matches[1] . sprintf( __( 'By %s', 'wporg' ), $matches[2] ) . $matches[3];
}
return $content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@
<div class="wp-block-group entry-meta">
<!-- wp:post-date /-->

<!-- wp:group {"style":{"spacing":{"blockGap":"0.5em"}},"layout":{"type":"flex","flexWrap":"nowrap"}} -->
<div class="wp-block-group">
<!-- wp:paragraph -->
<p>By</p>
<!-- /wp:paragraph -->

<!-- wp:post-author-name {"isLink":true} /-->
</div>
<!-- /wp:group -->
<!-- wp:post-author-name {"isLink":true} /-->
</div>
<!-- /wp:group -->
</div>
Expand Down

0 comments on commit e702884

Please sign in to comment.