I’m trying to add some string replacements to the_title() –
function format_title($content) {
$content = str_replace('&','&<br>', $content);
$content = str_replace('!','!<br>', $content);
return $content;
}
add_filter('the_title','format_title',11);
When I try and replace ampersands I get an additional “#038;” after the replacement (ASCII for ampersand), I’m not sure as to why this occurs (security reason?) or how to create a workaround. I’ve tried replacing “&” with “& amp ;” but with no effect.
The goal is to add line breaks at certain points of a title to create a better flow in the typography.
Both the database and the site has UTF8 encoding.
&
is essentially synonym of&
. Inthe_title
filterwptexturize()
runs with priority1
(important!) and makes this replacement.So by the time it gets to your
format_title()
at priority11
– instead of replacing lone&
symbol you replace (and break) chunk of&
character entity.So you can:
0
and it will run before texturize11
but replace$#38;
instead of just&
This can also be caused by
esc_url
. You should useesc_url_raw
instead, in this scenario.esc_url
has this piece of code:What
esc_url_raw
does is simply callingesc_url
and skipping that conditional by passing a different context as argument.Here is one more solution:-
It works for me.