0

I am trying to have my profile picture appear next to my name when I upload posts to my blog post page. Instead, I am getting a default no photo icon. Could you further assist me in this?

  • This is impossible to answer with the few details you provided. – RiddleMeThis Jan 16 at 20:11
0

Your profile picture is pulled in from the service Gravatar.com. If you do not have an account there, I'd suggest setting one up with the email you have set for your user on your WordPress site, and then it will display the correct image.

0

I'm not sure if this is exactly what you want to do, but I've used code similar to this in a theme before. It would be more helpful to see what code you've tried. The code below should allow you to use a custom headshot on a per author basis that's different from the gravatar/author icon.

NB - This isn't my original code. I pulled it from a tutorial, but I can't recall where exactly. If I did I'd source it. I did modify it some though. It involves hooking into several different actions.

// functions.php
// - hook into user profiles to create the form part for custom images
add_action('show_user_profile', 'add_headshot');
add_action('edit_user_profile', 'add_headshot');

// get a template template to add to the user's admin profile page
// this will be a set of inputs including a button and hidden input (see below)
function add_headshot($user) {
    require_once(THEME_DIR . '/partials/admin-custom-image.php');
}

// - hook into updating/saving the user meta data for custom images.
add_action('personal_options_update', 'save_additional_user_meta');
add_action('edit_user_profile_update', 'save_additional_user_meta');

// update the user meta data with your values from the $_POST object
// these will be set when the user's profile is saved.
function save_additional_user_meta($user_id) {
    if (!current_user_can('edit_user', $user_id)) return;

    update_user_meta($user_id, 'user_meta_image', $_POST['user_meta_image']);
    update_user_meta($user_id, 'user_meta_image_name', $_POST['user_meta_image_name']);
    update_user_meta($user_id, 'user_meta_image_id', $_POST['user_meta_image_id']);
}

You can then access the meta data on a per author basis like this in the loop

function get_author_data() {
    return array(
        'id' => get_the_author_meta('ID'),
        'url' => get_author_posts_url(get_the_author_meta('ID')),
        'first_name' => get_the_author_meta('first_name'),
        'last_name' => get_the_author_meta('last_name'),
        'description' => get_the_author_meta('description'),
        'headshot' => get_the_author_meta('user_meta_image')
    );
}

Here is a template to inject into the user admin profile page via the add_headshot function.

<h2><?php _e('Custom User Image', 'textdomain')?></h2>
<table class="form-table">
  <tr>
    <th>
      <?php _e('Custom Headshot', 'textdomain')?>
    </th>
    <td>
      <img width=600 
        height=400
        style="object-fit: cover; object-position: center;" 
        src="<?php
        $image_url = get_the_author_meta('user_meta_image', $user->ID);
        echo $image_url;
      ?>" />
  </tr>
  <tr>
    <th>
      <label for="uploadimage">Upload image</label>
    </th>
    <td>
      <input type='button'
        id="uploadimage" 
        class="additional-user-image button-primary" 
        value="<?php _e( 'Upload Image', 'textdomain' ); ?>" 
        />
    </td>
  </tr>
  <tr>
    <th>
      <label for="user_meta_image">Custom image URL</label>
    </th>
    <td>
      <input type="text" 
        name="user_meta_image" 
        id="user_meta_image" 
        value="<?php echo esc_url_raw( get_the_author_meta( 'user_meta_image', $user->ID ) ); ?>" class="regular-text" />
    </td>
  </tr>
  <tr>
    <th>
      <label for="user_meta_image_name">Custom image name</label>
    </th>
    <td>
      <input type="text" 
        name="user_meta_image_name" 
        id="user_meta_image_name" 
        value="<?php echo get_the_author_meta('user_meta_image_name', $user->ID); ?>" />
    </td>
  </tr>
  <tr>
    <th>
      <span class="description">
        <?php _e( 'Upload an additional image for your user profile.', 'textdomain' ); ?>
      </span>
    </th>
    <td>
      <input type="hidden" 
        id="user_meta_image_id" 
        name="user_meta_image_id" 
        value="<?php echo get_the_author_meta('user_meta_image_id', $user->ID); ?>" />
    </td>
  </tr>
</table>

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.