0

Based on code the awesome G.M. provided in an earlier question (Use Custom Fields to Create Guest Author Link), I now have a custom function that uses custom fields to support guest authors. The code changes the post author to display the guest author name and changes the author link to the guest author URL of my choosing. This is the code I've added to my theme's function.php file to support guest authors:

//Guest Author Handling - Uses custom fields for guest author name and URL. Changes the guest author name display and changes the author link to guest author URL of your choosing
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;
}

After playing with this, it occurred to me that it's possible to simplfy the guest author URL entry. The URL for my guest authors will always be formatted as follows:

http://yoursite.com/author/[AuthorName]

Since the URL will always follow this same pattern, what I need to know is how would I modify G.M.'s code so that I only need to enter the [AuthorName] in the custom field for the guest authors URL?

I hope this makes sense...

0

Now I've understand the question, and yes, it makes sense.

The filter additions and the third function do not change at all, what change are the first two functions.

There is 2 cases:

  1. [AuthorName] part in the url is equivalent sanitize_title($guestauthor) where $guestauthor is what is saved in the 'guest-author' custom field
  2. [AuthorName] part is different and indipendent from 'guest-author' custom field

In 1st case we can assume guest authors have always a custom url and you don't need the 'guest-url' custom field at all. The code become:

function guest_author_url($url) {
  global $post;
  $guest_url_suffix = sanitize_title( get_post_meta( $post->ID, 'guest-author', true ) );
  $guest_url = $guest_url_suffix ? site_url() . '/author/' .  $guest_url_suffix : null;
  if ( filter_var($guest_url, FILTER_VALIDATE_URL) ) {
    return $guest_url;
  }
  return $url;
}

function guest_author_link($name) {
  global $post;
  $guest_name = get_post_meta( $post->ID, 'guest-author', true );
  $guest_url = $guest_name ? site_url() . '/author/' . sanitize_title($guest_name) : null;
  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>';
  }
  return $name;
}

In 2nd case, in the 'guest_url' custom field you have to put the [AuthorName] part of the url. This give you a bonus possibility: if you don't put anything in this field no link will ever shown for the user. The code for this case is:

function guest_author_url($url) {
  global $post;
  $guest_url_suffix = get_post_meta( $post->ID, 'guest-url', true );
  $guest_url = $guest_url_suffix ? site_url() . '/author/' .  $guest_url_suffix : null;
  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_suffix = get_post_meta( $post->ID, 'guest-url', true );
  $guest_url = $guest_url_suffix ? site_url() . '/author/' .  $guest_url_suffix : null;
  $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;
}

New code is untested, but - unless any typo - it should work.

  • That did it!!! I used the second case and it worked perfectly!!! Thanks so much!! – Crystal Jones Aug 17 '13 at 23:16
0

Just in case someone got here looking for how to also add a Guest Description, here is my full implementation.

If follows the same logic as the other answers, I just added a get_the_author_description filter and a function to overwrite the author Bio info (description). As the other solutions above, you pass it as a custom field (see image attached).

enter image description here

This is the full implementation, adding the above solutions.

// Change Guest Author Name, Link and Description
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' );
add_filter( 'get_the_author_description', 'guest_author_description' );

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 '' . $guest_name . '';
  } elseif( $guest_name ) {
    return '' . $guest_name . '';
  }
  return $name;
}


function guest_author_description($description) {
  global $post;
  $guest_description = get_post_meta( $post->ID, 'guest-description', true );
  if ( $guest_description ) return $guest_description;
  elseif ( get_post_meta( $post->ID, 'guest-author', true ) ) {
    return '';
  }
  return $description;
}

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;
}
  • Please add proper explanation to what your code does, how it works and how it should be used. Answers should be more than just a code dump – Pieter Goosen Apr 11 '16 at 4:23
  • My bad. I just updated it with more info and an image. – Matheus Baumgart Apr 12 '16 at 3:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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