One User Avatar Shortcodes

Below, you’ll find a detailed guide on how to add the One User Avatar | User Profile Picture Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the One User Avatar | User Profile Picture Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the One User Avatar | User Profile Picture Plugin and the shortcodes it provides:

Plugin Icon
One User Avatar | User Profile Picture

"One User Avatar is a user-friendly WordPress plugin allowing users to easily update and manage profile pictures. Enhance your site's user profiles with this simple tool."

★★★★☆ (39) Active Installs: 60000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [avatar]
  • [avatar_upload]

One User Avatar [avatar] Shortcode

The One User Avatar shortcode is designed to display a user’s avatar on a WordPress site. It searches for the user by ID, login, slug, or email. It allows customization of size and alignment, and can link the avatar to a file, attachment, or URL. If no user is specified, it defaults to the post author or page slug. Shortcode: [avatar user=”” size=”96″ align=”” link=”” target=””] The shortcode also supports captions, and adjusts image size accordingly. It wraps the avatar in a link if specified, and sanitizes the output for safe rendering. Shortcode: [avatar content=”caption”]

Shortcode: [avatar]

Parameters

Here is a list of all possible avatar shortcode parameters and attributes:

  • user – Specifies the user whose avatar is to be displayed.
  • size – Determines the size of the avatar in pixels.
  • align – Sets the alignment of the avatar image.
  • link – Defines the link type or URL associated with the avatar.
  • target – Sets the target attribute for the link around the avatar.

Examples and Usage

Basic example – Display the avatar of the current user

[avatar user="current" /]

Advanced examples

Display the avatar of a specific user by ID, with a size of 200px.

[avatar user="2" size="200" /]

Display the avatar of a user with a specific username, aligned to the right.

[avatar user="john_doe" align="right" /]

Display the avatar of a user with a specific email, linked to their website, opening in a new tab.

[avatar user="johndoe@example.com" link="https://example.com" target="_blank" /]

Display the avatar with a custom size, and a caption.

[avatar size="custom" user="john_doe"]This is a caption[/avatar]

PHP Function Code

In case you have difficulties debugging what causing issues with [avatar] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'avatar', array( $this, 'wpua_shortcode' ) );

Shortcode PHP function:

function wpua_shortcode( $atts, $content = null ) {
		global $all_sizes, $blog_id, $post, $wpdb;

		// Set shortcode attributes
		extract( shortcode_atts( array(
			'user'   => '',
			'size'   => '96',
			'align'  => '',
			'link'   => '',
			'target' => '',
		), $atts ) );

		// Find user by ID, login, slug, or e-mail address
		if ( ! empty( $user ) ) {
			if ( 'current' == $user ) {
				$user = wp_get_current_user();
			} else {
				$user = is_numeric( $user ) ? get_user_by( 'id',    $user ) : get_user_by('login', $user);
				$user = empty( $user )      ? get_user_by( 'slug',  $user ) : $user;
				$user = empty( $user )      ? get_user_by( 'email', $user ) : $user;
			}
		} else {
			// Find author's name if id_or_email is empty
			$author_name = get_query_var( 'author_name' );

			if( is_author() ) {
				// On author page, get user by page slug
				$user = get_user_by( 'slug', $author_name );
			} else {
				// On post, get user by author meta
				$user_id = get_the_author_meta( 'ID' );
				$user    = get_user_by( 'id', $user_id );
			}
		}

		// Numeric sizes leave as-is
		$get_size = $size;

		// Check for custom image sizes if there are captions
		if ( ! empty( $content ) ) {
			if ( in_array( $size, $all_sizes ) ) {
				if ( in_array( $size, array( 'original', 'large', 'medium', 'thumbnail' ) ) ) {
					$get_size = ( $size == 'original' ) ? get_option( 'large_size_w' ) : get_option( $size.'_size_w' );
				} else {
					$get_size = $_wp_additional_image_sizes[$size]['width'];
				}
			}
		}

		// Get user ID
		$id_or_email = ! empty( $user ) ? $user->ID : 'unknown@gravatar.com';

		// Check if link is set
		if ( ! empty( $link ) ) {
			// CSS class is same as link type, except for URL
			$link_class = $link;

			if ( 'file' == $link ) {
				// Get image src
				$link = get_wp_user_avatar_src( $id_or_email, 'original' );
			} elseif ( $link == 'attachment' ) {
				// Get attachment URL
				$link = get_attachment_link( get_the_author_meta( $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar', $id_or_email ) );
			} else {
				// URL
				$link_class = 'custom';
			}

			// Link target
			if ( ! in_array( $target, array( '_blank', '_self', '_parent', '_top' ) ) ) {
				$target = '';
			}

			// Wrap the avatar inside the link
			$html = sprintf(
				'<a href="%s" class="wp-user-avatar-link wp-user-avatar-%s"%s>%s</a>',
				esc_url( $link ),
				esc_attr( $link_class ),
				( $target ? sprintf( ' target="%s"', esc_attr( $target ) ) : '' ),
				get_wp_user_avatar( $id_or_email, $get_size, $align )
			);
		} else {
			$html = get_wp_user_avatar( $id_or_email, $get_size, $align );
		}

		// Check if caption is set
		if ( ! empty( $content ) ) {
			// Get attachment ID
			$wpua    = get_user_meta( $id_or_email, $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar', true );

			// Clean up caption
			$content = trim( $content );
			$content = preg_replace( '/\r|\n/', '', $content );
			$content = preg_replace( '/<\/p><p>/', '', $content, 1 );
			$content = preg_replace( '/<\/p><p>$/', '', $content );
			$content = str_replace( '</p><p>', '<br /><br />', $content );

			$avatar  = do_shortcode( image_add_caption( $html, $wpua, $content, $title = '', $align, $link, $get_size, $alt = ''));
		} else {
			$avatar  = $html;
		}

		return wp_kses_post( $avatar );
	}

Code file location:

one-user-avatar/one-user-avatar/includes/class-wp-user-avatar-shortcode.php

One User Avatar [avatar_upload] Shortcode

The One User Avatar shortcode allows authorized users to upload avatars. It verifies user permissions, identifies the user, and displays a form for valid users. Errors are handled gracefully and successful updates are confirmed.

Shortcode: [avatar_upload]

Parameters

Here is a list of all possible avatar_upload shortcode parameters and attributes:

  • user – Specifies the user by ID, login, slug, or email

Examples and Usage

Basic example – Allows the user to upload an avatar image using the shortcode ‘avatar_upload’ without any additional parameters.

[avatar_upload /]

Advanced examples

Using the shortcode to allow a specific user to upload an avatar by referencing their user ID. The avatar upload form will be displayed only if the user with the given ID is logged in and has the required permissions.

[avatar_upload user=3 /]

Using the shortcode to allow a specific user to upload an avatar by referencing their username. The avatar upload form will be displayed only if the user with the given username is logged in and has the required permissions.

[avatar_upload user="john_doe" /]

Using the shortcode to allow a specific user to upload an avatar by referencing their email. The avatar upload form will be displayed only if the user with the given email is logged in and has the required permissions.

[avatar_upload user="johndoe@example.com" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [avatar_upload] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'avatar_upload', array( $this, 'wpua_edit_shortcode' ) );

Shortcode PHP function:

function wpua_edit_shortcode( $atts ) {
		global $current_user, $errors, $wp_user_avatar, $wpua_allow_upload;

		// Shortcode only works for users with permission
		if ( $wp_user_avatar->wpua_is_author_or_above() || ( 1 == (bool) $wpua_allow_upload && is_user_logged_in() ) ) {
			extract( shortcode_atts( array( 'user' => '' ), $atts ) );

			// Default user is current user
			$valid_user = $current_user;

			// Find user by ID, login, slug, or e-mail address
			if ( ! empty( $user ) ) {
				$get_user = is_numeric( $user ) ? get_user_by( 'id',    $user ) : get_user_by( 'login', $user );
				$get_user = empty( $get_user )  ? get_user_by( 'slug',  $user ) : $get_user;
				$get_user = empty( $get_user )  ? get_user_by( 'email', $user ) : $get_user;

				// Check if current user can edit this user
				$valid_user = current_user_can( 'edit_user', $get_user->ID ) ? $get_user : null;
			}

			$output = '';

			// Show form only for valid user
			if ( $valid_user ) {
				// Save
				if (
					( isset( $_POST['submit'] ) && $_POST['submit'] )
					&&
					( isset( $_POST['wpua_action'] ) && 'update' == $_POST['wpua_action'] )
					&&
					( isset( $_POST[ '_wpnonce'] ) && wp_verify_nonce( $_POST[ '_wpnonce'], 'update-user_' . $valid_user->ID ) )
				) {
					ob_start();

					do_action( 'wpua_update', $valid_user->ID );

					// Check for errors
					$errors = $this->wpua_edit_user( $valid_user->ID );

					// Errors
					if ( isset( $errors ) && is_wp_error( $errors ) ) {
						printf( '<div class="error"><p>%s</p></div>', implode( "</p>\n<p>", $errors->get_error_messages() ) );
					} else {
						printf( '<div class="success"><p><strong>%s</strong></p></div>', __( 'Profile updated.', 'one-user-avatar' ) );
					}

					$output .= ob_get_clean();
				}

				// Edit form
				$output .= $this->wpua_edit_form( $valid_user );

				return wp_kses( $output, array_merge( wp_kses_allowed_html( 'post' ), array(
					'form'  => array(
						'id'      => true,
						'class'   => true,
						'action'  => true,
						'class'   => true,
						'method'  => true,
						'enctype' => true,
					),
					'input' => array(
						'type'    => true,
						'name'    => true,
						'id'      => true,
						'class'   => true,
						'value'   => true,
					),
				) ) );
			}
		}
	}

Code file location:

one-user-avatar/one-user-avatar/includes/class-wp-user-avatar-shortcode.php

Conclusion

Now that you’ve learned how to embed the One User Avatar | User Profile Picture Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *