• Resolved smiranda

    (@smiranda)


    Since PublishPress has plugins for featured images and authors with profile images, it would be the best entity to develop a simple script to use profile images as the author’s post featured image. Possible? I’ll be experimenting with a script put together by AI.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Steve Burge

    (@stevejburge)

    Hi @smiranda. Thanks for using PublishPress Authors.

    You can use the plugin itself to show the image for the featured author.

    However, if this “post featured image” feature is part of a theme, it will require some custom code.

    Plugin Author Steve Burge

    (@stevejburge)

    @smiranda We got your email to our support desk too. That phrasing was a little different. If you want to use the author profile as the featured image, try this code. You can add it your theme’s functions.php or via one of the plugins that allow you to add code via the admin area.

    function set_author_avatar_as_featured_image($post_id) {
    // Avoid infinite loop
    remove_action('save_post', 'set_author_avatar_as_featured_image');

    // Only run for posts
    if (get_post_type($post_id) !== 'post') {
    return;
    }

    // Check if post already has a featured image
    if (has_post_thumbnail($post_id)) {
    return;
    }

    // Load PublishPress Authors functions
    if (!function_exists('get_coauthors')) {
    return;
    }

    // Get the first author
    $authors = get_coauthors($post_id);
    if (empty($authors) || !is_array($authors)) {
    return;
    }

    $author = $authors[0];

    // Get avatar URL (PublishPress uses user meta or term meta depending on author type)
    $avatar_url = get_avatar_url($author->ID, ['size' => 512]);
    if (!$avatar_url) {
    return;
    }

    // Upload the avatar and attach as post thumbnail
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/media.php');

    // Sideload the image
    $tmp = download_url($avatar_url);

    if (is_wp_error($tmp)) {
    return;
    }

    $file_array = [
    'name' => basename($avatar_url),
    'tmp_name' => $tmp
    ];

    $id = media_handle_sideload($file_array, $post_id);

    // Set as featured image if upload worked
    if (!is_wp_error($id)) {
    set_post_thumbnail($post_id, $id);
    }

    add_action('save_post', 'set_author_avatar_as_featured_image');

    function set_author_avatar_as_featured_image($post_id) {
    // Avoid infinite loop
    remove_action('save_post', 'set_author_avatar_as_featured_image');

    // Only run for posts
    if (get_post_type($post_id) !== 'post') {
    return;
    }

    // Check if post already has a featured image
    if (has_post_thumbnail($post_id)) {
    return;
    }

    // Load PublishPress Authors functions
    if (!function_exists('get_coauthors')) {
    return;
    }

    // Get the first author
    $authors = get_coauthors($post_id);
    if (empty($authors) || !is_array($authors)) {
    return;
    }

    $author = $authors[0];

    // Get avatar URL (PublishPress uses user meta or term meta depending on author type)
    $avatar_url = get_avatar_url($author->ID, ['size' => 512]);
    if (!$avatar_url) {
    return;
    }

    // Upload the avatar and attach as post thumbnail
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/media.php');

    // Sideload the image
    $tmp = download_url($avatar_url);

    if (is_wp_error($tmp)) {
    return;
    }

    $file_array = [
    'name' => basename($avatar_url),
    'tmp_name' => $tmp
    ];

    $id = media_handle_sideload($file_array, $post_id);

    // Set as featured image if upload worked
    if (!is_wp_error($id)) {
    set_post_thumbnail($post_id, $id);
    }

    add_action('save_post', 'set_author_avatar_as_featured_image');
    }
    add_action('save_post', 'set_author_avatar_as_featured_image');

    Plugin Author Riza Prihananto

    (@rizaprihananto)

    Hi @smiranda,

    We haven’t heard from you in a week so are closing this discussion for now. Please post back again if you have more questions

    Thread Starter smiranda

    (@smiranda)

    I’d love for your code to work. It doesn’t at first try. Can you help determine the reason?

    Plugin Author Riza Prihananto

    (@rizaprihananto)

    Hi @smiranda ,

    If that code does not work, could you try to use the following code?

    add_action('save_post', 'set_author_avatar_as_featured_image', 10, 3);

    function set_author_avatar_as_featured_image($post_id, $post, $update) {
    // Only run for posts (you can modify this for other post types)
    if ($post->post_type !== 'post') {
    return;
    }

    // Skip if this is an autosave or revision
    if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
    return;
    }

    // Skip if featured image already exists (optional - remove this if you want to override)
    if (has_post_thumbnail($post_id)) {
    return;
    }

    // Get the post author
    $author_id = $post->post_author;

    // Get author's avatar and set as featured image
    $attachment_id = get_or_create_avatar_attachment($author_id);

    if ($attachment_id) {
    set_post_thumbnail($post_id, $attachment_id);
    }
    }

    /**
    * Get or create attachment from author's avatar
    */
    function get_or_create_avatar_attachment($user_id) {
    // Check if we already have an attachment for this user's avatar
    $existing_attachment = get_user_meta($user_id, '_avatar_attachment_id', true);

    if ($existing_attachment && wp_attachment_is_image($existing_attachment)) {
    return $existing_attachment;
    }

    // Get the avatar URL
    $avatar_url = get_avatar_url($user_id, array('size' => 512));

    // Skip default avatars (Gravatar default images)
    if (strpos($avatar_url, 'd=mm') !== false || strpos($avatar_url, 'd=blank') !== false) {
    return false;
    }

    // Download and create attachment
    $attachment_id = upload_avatar_to_media_library($avatar_url, $user_id);

    if ($attachment_id) {
    // Store the attachment ID in user meta for future use
    update_user_meta($user_id, '_avatar_attachment_id', $attachment_id);
    }

    return $attachment_id;
    }

    /**
    * Upload avatar image to WordPress media library
    */
    function upload_avatar_to_media_library($image_url, $user_id) {
    // Include required WordPress functions
    require_once(ABSPATH . 'wp-admin/includes/media.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/image.php');

    // Get user info for filename
    $user_info = get_userdata($user_id);
    $username = sanitize_file_name($user_info->user_login);

    // Download the image
    $temp_file = download_url($image_url);

    if (is_wp_error($temp_file)) {
    return false;
    }

    // Prepare file array
    $file_array = array(
    'name' => $username . '-avatar.jpg',
    'tmp_name' => $temp_file
    );

    // Upload to media library
    $attachment_id = media_handle_sideload($file_array, 0, 'Author avatar for ' . $user_info->display_name);

    // Clean up temp file
    if (file_exists($temp_file)) {
    unlink($temp_file);
    }

    if (is_wp_error($attachment_id)) {
    return false;
    }

    return $attachment_id;
    }

    /**
    * Optional: Bulk update existing posts to use author avatars as featured images
    * Run this once by visiting yoursite.com/?bulk_update_author_avatars=1
    */
    add_action('init', 'bulk_update_author_avatars');

    function bulk_update_author_avatars() {
    // Only run if the URL parameter is set and user is admin
    if (!isset($_GET['bulk_update_author_avatars']) || !current_user_can('manage_options')) {
    return;
    }

    $posts = get_posts(array(
    'numberposts' => -1,
    'post_type' => 'post',
    'meta_query' => array(
    array(
    'key' => '_thumbnail_id',
    'compare' => 'NOT EXISTS'
    )
    )
    ));

    $updated_count = 0;

    foreach ($posts as $post) {
    $attachment_id = get_or_create_avatar_attachment($post->post_author);

    if ($attachment_id) {
    set_post_thumbnail($post->ID, $attachment_id);
    $updated_count++;
    }
    }

    wp_die("Updated {$updated_count} posts with author avatars as featured images.");
    }

    /**
    * Optional: Clear stored avatar attachments when user profile is updated
    * This ensures fresh avatars if the user changes their Gravatar
    */
    add_action('profile_update', 'clear_stored_avatar_attachment');

    function clear_stored_avatar_attachment($user_id) {
    delete_user_meta($user_id, '_avatar_attachment_id');
    }

    /**
    * Optional: Add admin notice to explain the feature
    */
    add_action('admin_notices', 'author_avatar_featured_image_notice');

    function author_avatar_featured_image_notice() {
    $screen = get_current_screen();

    if ($screen->id === 'post' || $screen->id === 'edit-post') {
    echo '<div class="notice notice-info is-dismissible">
    <p><strong>Auto Featured Image:</strong> If no featured image is set, the author\'s avatar will automatically be used as the featured image when the post is saved.</p>
    </div>';
    }
    }

    You can add on the function.php or you can also use Code Snippet plugin to apply the code.

    Thread Starter smiranda

    (@smiranda)

    The second code set doesn’t insert the author’s profile image as their post featured image. Could it be that your code only applies to the logged in author, an not the author if changed in the posting process?

    Plugin Author Steve Burge

    (@stevejburge)

    Hi @smiranda

    These were suggestions from AI sources, such as Claude and ChatGPT. They seem to be able to extend our plugin successfully. You’re welcome to give those a try to solve this for your requirements.

    Our support for Authors Pro members does include custom code from our team. For Free users here in the WordPress forums we’re able make suggestions like these.

    • This reply was modified 1 year, 2 months ago by Steve Burge.
    Thread Starter smiranda

    (@smiranda)

    Would the Pro version enable the team to provide custom code as part of the benefit, or would that be for an extra fee?

    Plugin Author Steve Burge

    (@stevejburge)

    Hi @smiranda. Yes, that’s correct. The Pro support can go further and get more specific.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘is there a way to use profile images as author’s post featured image’ is closed to new replies.