Can I display Avatars for multisite using a shortcode?

Is there a way to display the user avatar using a shortcode? I need this to create a profile page for coursepress subscribers, as the current coursepress profile page doesn't include all the wp profile fields.

  • Vaughan

    Hi Robert,

    Hope you're well?

    Out of the box there isn't a shortcode to display an avatar, however you could do this with a custom mu-plugin.

    I haven't tested this, but

    Create a file called avatar_shortcode.php

    Then add the below code to this file.

    Now upload the file to /wp-content/mu-plugins (if mu-plugins doesn't exist, create it)

    <?php
    function wpmudev_display_avatar($attr) {
    	  extract(shortcode_atts(array(
    		       "user_id" => '',
    	  ), $attr, 'avatar' ));
    	  $get_avatar = get_avatar( $attr['user_id'], 150 );
    	   ?>
    <style type="text/css">
    .get_avatar{display:inline-block}.get_avatar img.avatar.photo{display:block}.get_avatar.circle img.avatar.photo{border-radius:50%;-moz-border-radius:50%;-webkit-border-radius:50%}.get_avatar.rounded img.avatar.photo{border-radius:5%;-moz-border-radius:5%;-webkit-border-radius:5%}
    </style>
    <?php
    	  return '<span>'.$get_avatar.'</span>';
    }
    add_shortcode ('avatar', 'wpmudev_display_avatar');
    }

    All being well, you should now be able to display the avatar with shortcode

    [avatar user_id=123]

    where 123 is the user_id

    Hope this helps

    You could also try the following plugin:

    https://wordpress.org/plugins/avatar-shortcode/

    Hope this helps

  • Vaughan

    Hi robert,

    We can certainly adjust the code I gave above for getting the current user id. However, if you do that, it would display the current users avatar when viewing someone elses profile.

    With the avatar plugin, do you happen to have WP_DEBUG set to true in wp-config.php?

    If so, can you set it to false and that should get rid of that error.

    define( 'WP_DEBUG', false );

    Hope this helps