While trying to show the author bio on the single posts on WordPress
This post isn’t about adding html tags to the biographical info btw, it’s about adding linebreaks only. Yes both are different concepts to be done, although allowing html tags would allow you to add line breaks too by using the <br /> tag.
How to add linebreak in WordPress biographical info
Well, first of all goto the “single.php” file of your theme files and see how the author bio is displayed. There can be several ways, however one of the common ones is:
<?php the_author_meta( 'description' ); ?>
Note that we’re using “the_author_meta()” not the “get_the_author_meta()” function for WordPress. What’s the difference between the two? well the one with “get_” doesn’t echo it directly, instead simply gets it, however the first one echos the result at that place too.
So if we were to use “get_the_author_meta()” then it would look like this:
<?php echo get_the_author_meta( 'description' ); ?>
We have to add echo to it so that it prints out the output of the function.
But why do we need this step? why not simply use the the_author_meta()? well because we’re going to modify the output of the author description before it gets printed on the page via echo!
We’re going to change the newline of the description to the “<br />” html tag by using “nl2br” like this:
<?php echo nl2br(get_the_author_meta( 'user_description' )); ?>
Now the output will be same as we formatted it in the dashboard, user profile settings page.
Note that WordPress allows <a> tags in the author bio too so you can add your links to the author description easily.
Why not to allow html tags in author bio?
If you have multiple authors on your WordPress blog, allowing the html tags and removing the sanitization can allow the authors to alter the webpage outlook by having weird html or even worse by using iframes. So it’s not a good idea to empower all your authors with this thing therefore it’s not recommended to allow html tags in author bio in WordPress.