The Entry Archive
How to Get Rid of WordPress’ Pesky get_the_url Problem
A few days ago I introduced a method by which multiple authors in a WordPress blog can have their photos automatically generated next to each of their published blog entries. Today I want to go a step further and delve into an issue that I ran into while designing another blog that is primarily written by one person and contributed to by various guest authors from time to time.
In the event that a guest author is generous enough to contribute to your blog, it is imperative that you show some love to them and provide a link back to their website (if they have one). Over time, with enough guest bloggers who may or may not have their own website, you will want to develop a system to manage the display and accuracy of the outgoing links to the other writers’ web domains.
“Well, that’s simple,” you say. “I’m just going to employ the get_the_url() WordPress template tag and all will be grand!” Not so fast, buddy!
The Problem
If a WordPress author does not have a website, one would think that the “URL� field in WordPress’ User Profile area would be blank. Unfortunately, this is not the case.

Every author who does not have their own URL is assigned the value ‘http://’ in their profile. This becomes problematic when trying to automate the conditional output of a writer’s website based on the value of their User Profile URL in the WordPress administrative area.
To illustrate this point further, take notice of what happens when the following lines of code are used to automate the process of including a guest blogger’s URL at the top or bottom of their entry.
This is an article written by guest blogger
<a href="<?php echo get_the_author_url(); ?>">
<?php the_author_firstname(); ?>
<?php the_author_lastname(); ?></a>.
(Note: There is a bit of PHP redundancy here to clearly identify each WordPress template tag that is being used.)

As you can see, the above code doesn’t work for authors who do not have their own website. The ‘get_author_url()’ template tag outputs the value ‘http://’ and gives the illusion that an author who does not have a URL does in fact have a website. The above bit of code produces an ugly set of broken links for your crew of guest bloggers who are not fortunate enough to have their own website.
The Fix
In order to properly display hyperlinks if a guest author does indeed have a website or display plain text if she does not have a URL, you will need to use the following lines of code in your WordPress theme:
This is an article written by guest blogger<?php $a=get_the_author_url();if ($a!="http://") { ?><a href="<?php echo $a; ?>"><?php the_author_firstname(); ?>
<?php the_author_lastname(); ?></a><?php }if ($a=="http://")echo get_the_author();?>
You can also download this code snippet (.txt format).
The PHP variable ‘$a’ is a temporary placeholder for the author URL. The above PHP code checks to see if the value of $a is equal to ‘http://’, and if it is, displays only the author name. If the guest author does in fact have their own URL, it will be properly displayed.
The End Result
After you have implemented the above code in your WordPress blog, you will have no more problems with broken author links in the event that a guest blogger does not have their own URL. Guest bloggers who do have their own websites will get the proper attribution that they deserve, and those without their own websites will simply have their names displayed (no broken link included).

Now all you have to do is invite your buds along for the ride and worry no more about who does or who does not have their own domain!
Post Date: March 8, 2007
Leave a Reply