Archived topic
change 'umlauts' in title/h1
7 replies · Started by Axel on November 14, 2018
hi there,
for a special case i am forced to replace german 'umlaute' in the title (H1) to their alternative expression from e.g. "ö" to "oe" etc.
Found a solution that is working:
function td_filter_post_titles_oe( $title ) {
return str_replace( 'ö', 'oe', $title );
}
add_filter( 'the_title', 'td_filter_post_titles_oe' );
function td_filter_post_titles_ue( $title ) {
return str_replace( 'ü', 'ue', $title );
}
add_filter( 'the_title', 'td_filter_post_titles_ue' );
function td_filter_post_titles_ae( $title ) {
return str_replace( 'ä', 'ae', $title );
}
but the whole thing has a little downside - and that is the circumstance that this changes all titles - even those in backend dashboard... which is not that ideal.
the goal would be to restrict the replacement only to H1-elements (on front-end). but i am completely struggling in every which way to get it done.
so my question would be if anyone has ever had a same demand and knows a way to get it done better?
Hi there,
You can try something like this:
add_filter( 'the_title', function( $title ) {
if ( ! is_admin() && in_the_loop() ) {
return str_replace( 'ö', 'oe', $title );
}
return $title;
} );
hi tom,
sadly this gives me a white screen ... dunno why :(
Ah sorry - try now.
dear tom,
thank you for your efforts - the code is working now.
but theres still some limitation. while the titles are allright in backend now (which is great to prevent user-side confusions), they still apply to all titles - not only to H1.
further on, i had to delete the && in_the_loop() part, because it didnt apply to page/post titles from elementor single views for cpts e.g.
so without the loop-definition i am quite happy already, but for an optimized solution still searching for a way to only apply it to H1 tags ...
wont steal your time... but if theres any idea... :)
kudos
the_title filter is used everywhere in WordPress unfortunately (menu items, widget titles etc..).
Unfortunately, there isn't a way to only target the H1 elements.
... i feared the answer would be like this.
big thanks anyway. really happy with your proposal and your support.
No problem :)