How to Rewrite Guest Author Name with Custom Fields in WordPress

Posted on February 25th, 2010 by in Tutorials | 37 Comments  
How to Rewrite Guest Author Name with Custom Fields in WordPress

Have you ever had a guest post on your blog? How do you go about giving the author full credit? We have seen many sites where they add an author profile box either above the post or below the post. But even with the guest author box, the author name field would still show the site author. Some bloggers create entire new author profiles for their guest authors in order to display the right name. We think that it is pointless to create extra user profiles if you know that this guest author will only post once.

The trick we are sharing in this article will allow you to show guest author’s name by simply entering a custom field in your post.

Open your functions.php file and paste the codes below:

    add_filter( 'the_author', 'guest_author_name' );
    add_filter( 'get_the_author_display_name', 'guest_author_name' );

    function guest_author_name( $name ) {
    global $post;

    $author = get_post_meta( $post->ID, 'guest-author', true );

    if ( $author )
    $name = $author;

    return $name;
    }

Now every time that you are posting a guest post, just make sure that you add a custom field called guest-author and it will replace the author’s name with the text that you put in that custom field.

Example:

Rewrite Guest Author's Name using Custom Fields in WordPress

About

Editorial Staff at WPBeginner mainly Syed and David.

Post comment as twitter logo facebook logo
Sort: Newest | Oldest
larsbachmann 5 pts

Hello again. I managed to write a code that could replace the description and email also.

http://www.larsbachmann.dk/gaesteblogger-og-custom...

larsbachmann 5 pts

Great article. I'm using the author fields to show my avatar, name and author description. Is there an easy way to customize this code to also rewrite gravatar and author description? Thx.

AndreaAlden 5 pts

Would be so nice if I knew WHERE on the functions.php page to paste this code...Do I need to add <?php> tags around it to make it work? Tried <?php> tags and pasting under "/** * Prints HTML with meta information for the current post-date/time and author. * * @since Twenty Ten 1.0 */" in functions.php file. Neither produced results when I refreshed the edit page in wordpress...Where is the guest author supposed to show up on the page?

jeffshinn 5 pts

I tested this in Wordpress 3.2.1 with a custom template built off twentyten and it works flawlessly!

Thanks for your contribution!

Athif 5 pts

wpbeginner :

Hi,

Is it compatible with wp Version 3.2.1? I added the code to my thesis 1.8.2 but it does not work. I cannot see the field in new posts draft though custom fields is selected in screen options

Athif 5 pts

Hi,

Is it compatible with wp Version 3.2.1? I added the code to my thesis 1.8.2 but it does not work. I cannot see the new field in new posts draft though custom fields is selected in screen options

MilindGupta 5 pts

I tried placing up the ! did it well no errors nothing but where is the custom field menu!??

where i'll put the name and stuff?

please reply its kinda urgent!

wpbeginner 50 pts

MilindGupta In your post write panel ... on the top right panel look at Screens Option ... then check custom fields there.

प्रतापसिंह 5 pts

wpbeginner how to change the name of author i want to write in something different language

How do you get the guest-author Custom Field to appear in RSS? It shows me as the author now since I created and published the post. The website shows the correct author, thanks to guest-author, but RSS still shows me. Is there a way to change that?

Yes, please look at our post about manipulating RSS. You can use one of the techniques mentioned in that article for this.

I'm overlooking something very obvious I guess.

The custom field has to be named guest-author

How whould the guest-author user be named? is that guest_author_name or something else? (I guess something else because that is not working for me)
(I'm creating a new website, for the moment you can find it on http://new.tenderfeelings.net)

If you look at the image in the article: It shows that the custom-field's name is guest-author and value: is Syed Balkhi. In the value area, you will add the guest-author's name.

ouch....i've jsut found the solucion
Note: In WordPress 3.1 – the core team wanted to make the Write Panel cleaner, so they unchecked the view button for Custom Fields. If you don’t see them, then click on the Screen Options Tab (in the top right hand corner), and check Custom Fields.

thank you

It looks like th 3.1 version does not allow this function anymore!

Great function!! Thanks for sharing :)

Maybe you guys can help me on one thing. I have 2 "official" authors on a blog and now I'm adding a guest author (not a new user, just a name on a custom field). The problem is that the guest author link is returning the official author's URL and consequently his posts. I need it to return all the guest author posts. How can I accomplish that??? (hope I made myself clear enough!).

Thanks!!!!!!

The best way to create author archives when you are in that condition ... is create a new account called Guest Authors, and then just post everything under there.

SRoberts 5 pts

Unfortunately, with the filter above applied, the title for that will have the custom guest author's name appear, but all posts from all guest authors will appear in the archive list. In other words, the title on the archive page will say "displaying all posts by (name of guest author)" rather than "displaying all posts by guest authors"

Great snippet, but with one fault though (which I hope you have a fix for). The snippet *does* replace my Guest Author user account with whatever name I define as a custom field, but when I click on that author name inside the post, I end up at an URL such as "/author/guestauthor", but the name displayed at the top of that page is not "Guest Author", as is the name of that user account, but instead the name I defined as a custom field for one of the posts.

I thought it would be solved by simple wrapping (is_single) around the snippet, so that it would only execute when viewing a post, but that doesn't work at all.

Any ideas?

Thanks :)

The whole idea behind this method is that you do not want to create a new profile for the user. Because a lot of guest posters are one-time posters. So you can just credit them with their name, and author signature at the bottom of the post. If they are regular posters, then this method is not for you.

I think the concern is that if you're using get_the_author in your template to link the author names to the author post archive, this links to whatever author is registered as the author of the post.

This technique, as posted, makes sense when you're running a single author blog and want to have guest authors, but when it's a multi-author site to begin with this doesn't work out quite as well.

Any thoughts on how that could be corrected so that it doesn't change the author's name on the archive page?

You can simply not link to the author archive page if it is Guest Post....

Is it possible to include a field for the author's description as well? I already have a plugin (wp-gravatar) that posts the author's description at the end of a post. However, I want to be able to post the guest author's description without having to create new accounts everytime. Is it possible to combine the method you described above to make it do this?

Yes, it is possible to do the guest author description via custom field. You can set a conditional statement if there is x custom field, then display the custom field text, if not then display wp-gravatar.

i'm going to jump in on this, since the custom field author hack has helped considerably ...

i'd like to populate the 'about the author' page as well, using the same method (custom field). i'm also not a coder; would i use a similar code as to the one used above?

thanks!

If you mean specific author profile pages, then no this cannot be used for that. If you mean a little box below the guest posts as a custom field, then sure a similar function could be used :)

i remain lost on how to do this (basically, do exactly what riki asked. i've fiddled with the code about a dozen or so times with no result. eeeeh.

I was really hoping this would work for me but hmmm didn't work :(

What is not working? We have this running on WPBeginner site and it works just fine. We are just calling a simple custom field.

I tried this too, pasting the code into the top of my includes/functions.php file. I got a php error, saying that something was an invalid filter. Does this work with Wordpress 3.0?

I hope I can get it to work, I'm porting a site to Wordpress form Joomla and this is the only thing I'm missing.

It's working fine with WP 3.0

Exactly what I needed! Thank you very much :-)

Thanks so much for this post - this was exactly what I've been looking for. Brought up my issue here and used a modified version of your code to fix it. Thanks again!

Thank you so much for this article; I was going nuts trying to get various writers sorted out for my website and now I can add author names so easily. I use custom fields for all my requirements which affected 'the_author()' and stopped author names being shown at all. The above has allowed me to have the authors appear and highlight different authors as they write.

Sorry for rambling! You have saved me a lot of work...thanks!
I'll also tweet it for you!

I'd like to try this suggestion but it would be helpful for this beginner to know where the "functions.php" file is located and then, exactly where to insert the code. I tried to edit the Theme Functions file (it said "functions.php in parentheses) but got a warning that this file cannot be edited. Is the file I want on the server?

thanks,
Carl

Hey Carl,

We have covered where functions.php file is located in our theme cheat sheet for beginners. You will need to insert this code right before ?> where the file ends.

Even though, I'm not a huge fan of custom fields period, I like the idea behind your snippet.
I was actually thinking about exactly this issue (adding a user for every guest author) just the other day.

Tweets about us: