WordPress.org

Ready to get started?Download WordPress

Forums

Random Default Avatar Function (13 posts)

  1. actionbasic
    Member
    Posted 1 year ago #

    I found this code :

    /* WpFreaks.com - Start of Default Avatar Modification */
    if ( !function_exists('fb_addgravatar') ) {
    function fb_addgravatar( $avatar_defaults ) {
    $myavatar = get_bloginfo('template_directory') . '/images/useravatar.png';
    $avatar_defaults[$myavatar] = 'Users';
    $myavatar2 = get_bloginfo('template_directory') . '/images/myavatar.png';
    $avatar_defaults[$myavatar2] = 'My Avatar';
    return $avatar_defaults; }
    add_filter( 'avatar_defaults', 'fb_addgravatar' ); }
    /* WpFreaks.com - End of Default Avatar Modification */

    And it seems like it will do some of the job. I need it to pick a random image from a folder. Is this possible?

    Any help would be great. Been searching and trying ideas for hours!

    Best,
    Brandon

  2. indybook
    Member
    Posted 1 year ago #

    $folder = scandir( dirname(__FILE__). '/random_images' );
    $avs = array();
    	foreach( $folder  as $pic ) {
    		if( strpos($pic, '.') ! == 0 ) {
    			$avs[] = $pic; }
    	}
    	$n = rand(0, sizeof($avs)-1);
    	$avatar = '<a href="bla.."><img src="'. get_bloginfo('template_directory') . '/random_images/' . $avs[$n] . '"></a>';
            return $avatar;

    whatever 'avatar_defaults' is. i'd use the filter with wp core 'get_avatar'

  3. actionbasic
    Member
    Posted 1 year ago #

    indyhook,

    thank you so much. I'n just not sure at all how to use that code. Is it used on it's own in the functions file?

    I really appreciate any help. This is killing me!

  4. indybook
    Member
    Posted 1 year ago #

    Hi actionbasic.

    no. this means to be a part of the function that you posted.
    place the code instead all you have between

    if ( !function_exists

    and

    }
    add_filter( 'avatar_defaults', 'fb_addgravatar' ); }

    i'm only not sure what 'avatar_defaults' function is to where the filter's added. so i'm not sure if the new part works as expected.
    where you actually have the initial code? at theme functions file? some plugin?
    let me know if it won't work

  5. bcworkz
    Member
    Posted 1 year ago #

    Hehe... simultaneous posting! Never mind.
    ---------------------------------------------
    You'll need to wrap the code in a function and hook the filter 'get_avatar'. Then it can go into the theme's functions.php file.

    function ab_rand_avatar() {
      //insert indybook's code here
    }
    add_filter('get_avatar', 'ab_rand_avatar');
  6. actionbasic
    Member
    Posted 1 year ago #

    Indybook / bcworkz,

    Thank you so much for the help. Again still learning and this is darn tricky for me for some reason! I've compiled the code based on both of your instructions. Does this look correct? Do I just place this in my functions file? I'm assuming I need to create the random_images folder in my template directory and fill it with avatar images? Do they have to be .png or anything? I don't see any file type in the code.

    function ab_rand_avatar() {
    $folder = scandir( dirname(__FILE__). '/random_images' );
    $avs = array();
    	foreach( $folder  as $pic ) {
    		if( strpos($pic, '.') ! == 0 ) {
    			$avs[] = $pic; }
    	}
    	$n = rand(0, sizeof($avs)-1);
    	$avatar = '<a href="bla.."><img src="'. get_bloginfo('template_directory') . '/random_images/' . $avs[$n] . '"></a>';
            return $avatar;
    add_filter('get_avatar', 'ab_rand_avatar');
  7. actionbasic
    Member
    Posted 1 year ago #

    I decided to take a shot and add the above code to my functions file. I get this error:

    Parse error: syntax error, unexpected '!' in /home/content/e/a/t/eatt5911/html/wp-content/themes/ari/functions.php on line 44

    That occurs in this line in the above code:

    if( strpos($pic, '.') ! == 0 ) {

    Am I still way off? :) Sorry guys, Tryin!

    Best,
    Brandon

  8. bcworkz
    Member
    Posted 1 year ago #

    Hi Brandon, I'm afraid I stuck my nose where it didn't belong in an attempt to be helpful. I quickly realized I misconstrued indybook's intent when we posted at the same time, thus the added "Never mind". I suggest you re-read indybook's second post and ignore my post entirely, then give it another go. He's unsure of the proper filter tag to use, so try both.

    As for the syntax error, I think the intent was no space between ! and == . Placing the code in functions.php file should be fine. The image folder with your avatar images should be subfolder /random_images/ in the template folder. Image format can be anything that a browser can show. Here I go again with my nose :)

  9. actionbasic
    Member
    Posted 1 year ago #

    Sorry guys to be a pain. Having no luck. IndyBook, not sure exactly what you meant here:

    place the code instead all you have between
    
    if ( !function_exists
    
    and
    
    }
    add_filter( 'avatar_defaults', 'fb_addgravatar' ); }

    I have inserted this code, trying to use PHP to get a random image...

    /* WpFreaks.com - Start of Default Avatar Modification */
    if ( !function_exists('fb_addgravatar') ) {
    function fb_addgravatar( $avatar_defaults ) {
    $myavatar = get_bloginfo('template_directory') . 'images/avatars/avatar_<?php echo(rand(1,5)); ?>.png';
    $avatar_defaults[$myavatar] = 'Users';
    return $avatar_defaults; }
    add_filter( 'avatar_defaults', 'fb_addgravatar' ); }
    /* WpFreaks.com - End of Default Avatar Modification */

    Shouldn't that work?

  10. bcworkz
    Member
    Posted 1 year ago #

    I'm afraid you might be stuck with me, so no :) You can't echo out to pass a value to PHP, nor should you define another <?php ?> block within another. Try this line instead:
    $myavatar = get_bloginfo('template_directory') . 'images/avatars/avatar_' . (string) rand(1,5) . '.png';

    I don't know what structure $avatar_defaults is supposed to be, but your line $avatar_defaults[$myavatar] = 'Users'; doesn't look right. I would think this is more likely correct:
    $avatar_defaults['Users'] = $myavatar;

  11. actionbasic
    Member
    Posted 1 year ago #

    Thank you bcworkz! I feel I'm super close.

    if ( !function_exists('fb_addgravatar') ) {
    function fb_addgravatar( $avatar_defaults ) {
    $myavatar = get_bloginfo('template_directory') . '/images/avatars/avatar_' . (string) rand(1,5) . '.png';
    $avatar_defaults[$myavatar] = 'Users';
    return $avatar_defaults; }
    add_filter( 'avatar_defaults', 'fb_addgravatar' ); }

    Adding that to my functions.php file will allow me to choose my custom avatar "Users" in the Discussions panel, and it WILL show up for commenters who do not have there own Avatar.

    However, so far it is using the same image for all commenters.

    Ideally, I would like to go to discussions, choose "Users" at the bottom of the Avatar list, and have a random image for each commenter who does not have their own. I'll be using small record covers. So you would see a comment section with say, 5 different record covers mixed in with people who have an actual Avatar.

  12. bcworkz
    Member
    Posted 1 year ago #

    I investigated this a bit, the hook you are using only adds the option for a different style of avatar. The only thing stored is a static path which is the same for all users. In order to have different images assigned to each user, you need to have a scheme where the same image is returned on every call based on the user email without storing it in a table.

    You then hook the 'get_avatar' filter to implement this scheme for each comment. This hook is not called if a gravatar solution exists. It is triggered from the end of get_avatar() in wp-includes/pluggable.php.

    A possible scheme would be to have eight different images. Do a hash of the email, and based on the last hexadecimal digit of the hash, assign one image to 0 and 1, another to 2 and 3, yet another to 4 and 5, etc.

    Sorry to give you a bunch more work, but now that you know some coding, it won't be so bad? At least you have assurance this scheme will result in a working solution.

    (BTW, love the record cover idea, that's going to be fun!)

  13. Frumph
    Member
    Posted 1 year ago #

    I know i'm a bit late to the party... but I just snippitted the code I use for inside my Easel theme (sans the options ability) which does what you want...

    http://frumph.net/2013/04/19/use-my-custom-default-random-avatars-code-in-any-theme/

Topic Closed

This topic has been closed to new replies.

About this Topic