If you would look at the source, you would see, that those symbols were converted.
I would guess it’s code sanitizing in the title, actually never heard that one could use html code in the title, but it looks, there is a solution:
http://wordpress.org/support/topic/html-in-post-title-1?replies=2
Thanks for the quick response! Unfortunately, I tried adding that code to functions.php in my child theme but it didn’t make any difference.
I realise putting HTML in post titles is probably not a common thing to do, but it was working this morning and for the last few years, at least. I’ll have a look at Twenty Twelve to see what it does differently.
A quick update: after testing on a vanilla WP blog, I can confirm that html in post/page titles works in Tiny Forge 1.5.2-3, but not in 1.5.4 (I was wrong in my OP, I must have been on 1.5.2-3). I can also confirm that the WP version has no effect.
I don’t think I changed anything in that matter and overall html code sanitizing is something done by the WP core (you could check change log).
If you will find exact cause, please tell, although I personally think that titles should be uniform, so in that regard I would not alter the default theme code.
I’ve found the problem: it’s the function tinyforge_the_title_trim in functions.php, specifically this line:
$title = esc_attr($title);
Which makes sense. If I comment out the whole function or just that line, then html in titles is not escaped and works as it should. (The function still seems to work without that line, maybe it is not necessary?)
Good catch!
Right, that function is used to sanitize the code:
http://codex.wordpress.org/Function_Reference/esc_attr
I guess you can comment it out 😉
Hi Tomas, I just updated to 1.5.4.2 and found the same problem. Is it possible to correct that problem in the child functions.php rather than editing the theme functions.php? I tried to do it but I couldn’t get it to work.
What would I need to add to the child functions.php?
Many thanks, David
Never imagined that HTML is used in the title, but it’s good that I receive feedback 😉
So in the next version function tinyforge_the_title_trim will be wrapped in if ! function_exists, to make it pluggable.
But until then you have to replace it in the parent themes functions.php. Replace whole 5.10 with this:
// 5.10 - Change title for protected and private posts - words "protected" and "private" are replaced by lock symbol.
if ( ! function_exists( 'tinyforge_the_title_trim' ) ) :
function tinyforge_the_title_trim($title) {
$title = esc_attr($title); // Sanitize HTML characters in the title. Comment out this line if you want to use HTML in post titles.
$findthese = array(
'#Protected:#',
'#Private:#'
);
$replacewith = array(
'<span class="icon-webfont el-icon-lock"></span>', // What to replace "Protected:" with
'<span class="icon-webfont el-icon-lock"></span> <span class="icon-webfont el-icon-user"></span>' // What to replace "Private:" with
);
$title = preg_replace($findthese, $replacewith, $title);
return $title;
}
endif;
add_filter('the_title', 'tinyforge_the_title_trim');
Then in your child theme’s function.php you will post the same code, but we will comment out that one line that does code sanitizing. This way your child theme will be good to go for the future:
// 1.3 - Allow HTML in post title. Original parent theme's function changes title for protected and private posts - words "protected" and "private" are replaced by lock symbol.
if ( ! function_exists( 'tinyforge_the_title_trim' ) ) :
function tinyforge_the_title_trim($title) {
// $title = esc_attr($title); // Sanitize HTML characters in the title. Comment out this line if you want to use HTML in post titles.
$findthese = array(
'#Protected:#',
'#Private:#'
);
$replacewith = array(
'<span class="icon-webfont el-icon-lock"></span>', // What to replace "Protected:" with
'<span class="icon-webfont el-icon-lock"></span> <span class="icon-webfont el-icon-user"></span>' // What to replace "Private:" with
);
$title = preg_replace($findthese, $replacewith, $title);
return $title;
}
endif;
add_filter('the_title', 'tinyforge_the_title_trim');
I forgot to warn about the down side of the HTML code in the title. When user enters not correct HTML in the content, it gets automatically fixed by WP, but if that would happen in the title, nobody would fix it, so user could break the design or styles of whole site.
It is also possible that in some rare cases it could affect security of the site.
Thank you Tomas, that worked perfectly. I don’t often need to style the post title, but it is nice to be able to do it. In this instance, the person whose blog it is wanted the word “Pink” in a title to be pink.
All the best, David