pulling member name from wordpress URL

Question

I’m trying to retrieve user information based on a url within wordpress. I’ve setup a page template for each of the wordpress users on the site. I’d like to say “contact userfirst userlast” and display their email.

the URL structure is www.example.com/memberpage/username.

My code is:

function retrieve_member_name (){
  $current_member = get_user_by( 'slug', basename($_SERVER["REQUEST_URI"]) );

  if ( ! $current_member->exists() ) {
    return;
  }
  if ($current_member->first_name){
    $fname = $current_member->first_name;
  } else {
    $fname = "";
  }

  if ($current_member->last_name){
    $lname = $current_member->last_name;
  } else {
    $lname = "";
  }
    $mname = $fname.' '.$lname;

    return $mname;
}
add_shortcode('membername','retrieve_member_name');

This works great, but if the member doesn’t exist I get an error that I’m using exists() incorrectly. What is the proper way to test for $current_member when I call get_user_by()?

0
rudtek 3 years 2019-10-29T13:10:32-05:00 0 Answers 58 views 0

Leave an answer

Browse
Browse