Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

We are posting articles authored by guest authors. The guest authors will not be logging in to the site, and we don't want to create users for these guest authors. We are also not interested in any of the guest blogger plugins for MANY reasons..

To display the guest authors name in the guest posts, I've added a function to my theme's functions.php file so that using a custom field, my guest authors name will appear in place of the name of WordPress user who posted the article.. This function is outlined here: http://www.wpbeginner.com/wp-tutorials/how-to-rewrite-guest-author-name-with-custom-fields-in-wordpress/

Using this, I am able to change the name of the author to display the guest author's name.. However the author link still points to the author page of the WordPress user who posted the article.

So now I'd like to do something similar with the author link. What I would like to do is for the author link to go to a URL that I will enter in a custom field like I enter the guest author's name.

This is the code I'm using, but it's not working. The author link still takes you to the author page for the WordPress user who posted the article.

add_filter( 'the_author_posts_link', 'guest_author_link', 10, 3);
add_filter( 'get_author_posts_url', 'guest_author_link', 10, 3);
add_filter( 'the_author_link', 'guest_author_link', 10, 3);
add_filter( 'get_author_link', 'guest_author_link', 10, 3);

function guest_author_link($link, $author_id, $author_nicename) {
  global $post;
  $url = get_post_meta( $post->ID, 'guest-url', true );
    if( $url ) {
        $link = $url;
    }
    return $link;
}
share|improve this question
    
initially i find it hard to believe that one function could handle that many filters. –  GhostToast Aug 16 '13 at 21:37
    
Right.... So jabs/digs at my incorrect code aside :), do you have a suggestion on how to achieve my objective?? –  Crystal Jones Aug 16 '13 at 22:49
1  
You didn't show code that triggers guest_author_link(). Have you tested your code to see if it is being executed? –  Charles Clarkson Aug 16 '13 at 23:14
    
And now you see that wasn't a jab but an assessment :) –  GhostToast Aug 17 '13 at 18:44
    
I see that GhostToast.. :) It's all good.. –  Crystal Jones Aug 17 '13 at 20:34

1 Answer 1

up vote 0 down vote accepted

Remove the filters and the function used in the tutorial you linked and replace them with this code:

add_filter( 'get_the_author_user_url', 'guest_author_url' ); 
add_filter( 'the_author', 'guest_author_link' ); 
add_filter( 'get_the_author_display_name', 'guest_author_name' );

function guest_author_url($url) {
  global $post;
  $guest_url = get_post_meta( $post->ID, 'guest-url', true );
  if ( filter_var($guest_url, FILTER_VALIDATE_URL) ) {
    return $guest_url;
  } elseif ( get_post_meta( $post->ID, 'guest-author', true ) ) {
    return '#';
  }
  return $url;
}

function guest_author_link($name) {
  global $post;
  $guest_url = get_post_meta( $post->ID, 'guest-url', true );
  $guest_name = get_post_meta( $post->ID, 'guest-author', true );
  if ( $guest_name && filter_var($guest_url, FILTER_VALIDATE_URL) ) {
    return '<a href="' . esc_url( $guest_url ) . '" title="' . esc_attr( sprintf(__("Visit %s&#8217;s website"), $guest_name) ) . '" rel="author external">' . $guest_name . '</a>';
  } elseif( $guest_name ) {
    return $guest_name;
  }
  return $name;
}

function guest_author_name( $name ) {
  global $post;
  $guest_name = get_post_meta( $post->ID, 'guest-author', true );
  if ( $guest_name ) return $guest_name;
  return $name;
}

Now you can use the_author_link() to see name and link for your guest author, but note that if your guest author has not a url the functions will show the name of the guest author linking the same page (href="#").

If you don't like this behavior, in your template file replace the_author_link() with the_author() and if your guest author has a name and a url the link is shown, otherwise you will see only the name.

Note that guest url must be a valid url (start with http:// or https://) or it will not be shown.

share|improve this answer
    
Hod d@mn G.M.!! That did it!!!! THANKS!!! I tried using the "Co-Authors Plus" plugin to do this, and I couldn't get the plugin to work, but this did the trick.. The one GOOD thing about the "Co-Authors Plus" plugin is it will create "author" pages for my guest authors which I can use in the custom field with this custom function.. Good stuff!!! –  Crystal Jones Aug 17 '13 at 3:26
    
One more question.. If the URL will ALWAYS follow a particular pattern with only ONE variation (ie: "yoursite.com/author/[author slug]"), How can this code be modified so that all I need to enter in the custom field is the variable (in this case the author slug)? –  Crystal Jones Aug 17 '13 at 17:40
    
Sorry, I don't full understand you question... maybe is better create another question, so you have more space to ask and explain and everyone (not only me) can easily answer. –  G. M. Aug 17 '13 at 19:10
    
I started to post a separate question, but didn't want to get slammed for posting a duplicate.. But it probably makes sense to do that.. :) Will post new question.. Standby.. :) –  Crystal Jones Aug 17 '13 at 20:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.