Skip to:
Content
Pages
Categories
Search
Top
Bottom

meta user field to load avatar URL


  • gabrieluno
    Participant

    @gabrieluno

    I’ve been looking for and testing codes without success for days, I need a function that creates a user meta field that loads the url of the buddypress avatar image, does anyone know how to do it?

    I would use the metafield for users to replace the profile image with an image url, and thus change the avatar of their author page. Thanks!

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

  • shanebp
    Moderator

    @shanebp

    Are you saying that you want to use BP avatars on WP author pages?
    If so, find the call to the avatar on the authors template.
    And replace it with something like this:

    <img class="" src="<?php 
         $authorUserID = get_the_author_meta('ID');  // get the user id of the post author
         echo bp_core_fetch_avatar ( 
              array(  'item_id'   => $authorUserID,   // output user id of post author
                'type'  => 'full',
                'html'  => FALSE  // FALSE = return url, TRUE (default) = return url wrapped with html and css classes
        ) 
    );

    gabrieluno
    Participant

    @gabrieluno

    As it was almost impossible for me to achieve what I wanted (due to my low knowledge), I switched to an easier alternative.

    Provide random avatars for users who did not upload an avatar, any of the codes below will work, but reloading the page reloads a different avatar. Would you know how to make the avatar save and not change when reloading the page? Greetings mate

    define ( 'BP_AVATAR_DEFAULT', 'uploads/Avatar-'.rand( 1 , 13 ).'.jpg' );

    $pics = array ('Avatar-1.jpg','Avatar-2.jpg','Avatar-3.jpg','Avatar-4.jpg');
    $picid = $pics[array_rand($pics,1)];
    define( 'BP_AVATAR_DEFAULT', BP_PLUGIN_URL . 'uploads/'. $picid);
    define( 'BP_AVATAR_DEFAULT_THUMB', BP_PLUGIN_URL . 'uploads/'. $picid );
    $picid = mt_rand(1, 15); //100 = No. of images
    define( 'BP_AVATAR_DEFAULT', BP_PLUGIN_URL . 'uploads/'.$picid.'.jpg' );

    shanebp
    Moderator

    @shanebp

    Perhaps this plugin would be useful to you:

    BP Local Avatars


    gabrieluno
    Participant

    @gabrieluno

    I’ll give up and use the typical mystery man, because I couldn’t adapt your plugin to load a local random avatar with the following code:

    'Avatar-folder/Avatar-name'.rand( 1 , 15 ).'.jpg'

    I’m sorry I bothered you @shanebp, thank you for your great work towards the bp community.


    shanebp
    Moderator

    @shanebp

    There are several filter hooks you could try.
    Look towards the bottom of function bp_core_fetch_avatar in this file:
    wp-content\plugins\buddypress\bp-core\bp-core-avatars.php

    For example, I would try this one first:

    function gabriel_random_avatar( $default_grav, $params ) {
    
         // $default_grav is an url
         // replace it with the full url to one of your preferred avatars
    
         return $default_grav;
    }
    add_filter('bp_core_avatar_default', 'gabriel_random_avatar', 99, 2 );
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar