Display Wordpress user's website URL in a div element

I’m working on a Wordpress project and need to show a user’s website URL inside a div on one of my pages. I’ve tried using the get_user_meta() function, but it’s not working as expected. Here’s what I’ve got so far:

<div>
  <?php 
    $author_id = 7;
    $meta_key = 'website_url';
    $is_single = true;
    $author_site = get_user_meta($author_id, $meta_key, $is_single); 
    echo '<p>' . $author_site . '</p>'; 
  ?>
</div>

Can someone help me figure out what I’m doing wrong? I want to make sure the URL shows up correctly in the div. Thanks for any advice!

John, I’ve dealt with this issue before. The problem might be that ‘website_url’ isn’t the default key for user websites in WordPress. Try using ‘user_url’ instead. Here’s a snippet that should work:

<div>
  <?php 
    $author_id = 7;
    $author_site = get_the_author_meta('user_url', $author_id);
    if ($author_site) {
      echo '<p>' . esc_url($author_site) . '</p>';
    } else {
      echo '<p>No website found for this user.</p>';
    }
  ?>
</div>

This code uses get_the_author_meta() which is more reliable for fetching user data. Also, it includes a check to handle cases where no URL is set. Remember to replace ‘7’ with the actual user ID you’re targeting.

I’ve encountered similar issues before, and here’s what worked for me:

First, make sure you’re using the correct user ID. If you’re on a single post page, you can use get_the_author_meta(‘ID’) to get the current author’s ID dynamically.

Also, consider using esc_url() to sanitize the URL output. This helps prevent potential security issues.

Here’s a modified version of your code that should work:

<div>
  <?php 
    $author_id = get_the_author_meta('ID');
    $author_site = get_the_author_meta('user_url', $author_id);
    if (!empty($author_site)) {
      echo '<p>' . esc_url($author_site) . '</p>';
    } else {
      echo '<p>No website URL found for this user.</p>';
    }
  ?>
</div>

This approach has been reliable in my projects. Let me know if you need any clarification!

hey john, have u checked if the ‘website_url’ meta key exists for that user? sometimes WP stores the URL under a different key. try using get_the_author_meta(‘user_url’, $author_id) instead. that usually works for me. let me know if it helps!