WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: Author Avatars List] Using username in shorcode (2 posts)

  1. sargespam
    Member
    Posted 5 years ago #

    I had a specific need to show author avatars using username (loginname) rather than email or userid. Since I could not find anything, I wrote my first plugin. I am not a programmer and the code below is probably not without problems - but I post it here in the hope that it may help someone.
    1. Copy the code below and save it as a php file.
    2. Upload the file to your plugins folder and activate it.
    3. Where you want the avatar to appear, use [showavatar username=loginname size=number] in the post.

    <?php
    /*
    Plugin Name: Show User Avatar
    Description: A plugin designed to provide shortcode ability in posts and comments using showavatar shortcode. Valid attributes are username=loginname  and size=number.
    Author: sargespam
    Version: 0.8
    */
    
    add_shortcode('showavatar', 'getuseravatar');
    function getuseravatar($atts, $content=null) {
    	    extract(shortcode_atts(array(
    		'username' => '',
    		'size' => '',
    		), $atts)) ;
    
    		$user = get_userdatabylogin($username);
    		$useridnum = $user->ID;
    
    	$output = get_avatar($useridnum, $size) ;
    	return $output;
    }
    
    ?>

    Alternately, I believe you can also use the following code in your theme's functions.php file rather than making it a plugin.

    function getuseravatar($atts, $content=null) {
    	    extract(shortcode_atts(array(
    		'username' => '',
    		'size' => '',
    		), $atts)) ;
    
    		$user = get_userdatabylogin($username);
    		$useridnum = $user->ID;
    
    	$output = get_avatar($useridnum, $size) ;
    	return $output;
    add_shortcode('showavatar', 'getuseravatar');

    http://wordpress.org/extend/plugins/author-avatars/

  2. Ben
    Member
    Posted 5 years ago #

    Nice, I'll take that as a feature suggestion for the Author Avatars List Plugin, if you don't mind :-)

Topic Closed

This topic has been closed to new replies.

About this Topic