Halloween Sale 15% off Ends in See the plans
PHP

About the Author Widget

Adds an "About the Author" sidebar widget that shows the current post author's photo, bio and a link to their other posts, and hides itself on other pages.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! class_exists( 'WPIDE_About_Author_Widget' ) ) {
    class WPIDE_About_Author_Widget extends WP_Widget {
        public function __construct() {
            parent::__construct( 'wpide_about_author', 'About the Author', array(
                'description'                 => 'The current post author\'s photo, bio and a link to their posts. Shows on single posts only.',
                'customize_selective_refresh' => true,
            ) );
        }

        public function widget( $args, $instance ) {
            if ( ! is_singular( 'post' ) ) {
                return;
            }

            $author_id = (int) get_post_field( 'post_author', get_queried_object_id() );
            $user      = $author_id ? get_userdata( $author_id ) : false;

            if ( ! $user ) {
                return;
            }

            $title = isset( $instance['title'] ) && '' !== $instance['title'] ? $instance['title'] : 'About the Author';
            $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
            $size  = ! empty( $instance['size'] ) ? absint( $instance['size'] ) : 80;
            $bio   = get_the_author_meta( 'description', $author_id );

            echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput -- theme markup.

            if ( '' !== (string) $title ) {
                echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput
            }

            echo '<div class="wpide-about-author" style="text-align:center;">';
            echo get_avatar( $author_id, $size, '', '', array( 'class' => 'wpide-about-author__avatar', 'extra_attr' => 'style="border-radius:50%;margin:0 auto .5em;display:block;"' ) );
            echo '<p style="margin:0 0 .5em;"><strong>' . esc_html( $user->display_name ) . '</strong></p>';

            if ( '' !== $bio ) {
                echo '<p style="margin:0 0 .5em;">' . wp_kses_post( $bio ) . '</p>';
            }

            echo '<p style="margin:0;"><a href="' . esc_url( get_author_posts_url( $author_id ) ) . '">' . esc_html( sprintf( 'More by %s', $user->display_name ) ) . '</a></p>';
            echo '</div>';
            echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput
        }

        public function form( $instance ) {
            $title = isset( $instance['title'] ) ? $instance['title'] : '';
            $size  = ! empty( $instance['size'] ) ? absint( $instance['size'] ) : 80;

            echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">Title:</label><input class="widefat" type="text" id="' . esc_attr( $this->get_field_id( 'title' ) ) . '" name="' . esc_attr( $this->get_field_name( 'title' ) ) . '" value="' . esc_attr( $title ) . '" /></p>';
            echo '<p><label for="' . esc_attr( $this->get_field_id( 'size' ) ) . '">Photo size (px):</label> <input class="tiny-text" type="number" min="32" max="256" id="' . esc_attr( $this->get_field_id( 'size' ) ) . '" name="' . esc_attr( $this->get_field_name( 'size' ) ) . '" value="' . esc_attr( $size ) . '" /></p>';

            return '';
        }

        public function update( $new_instance, $old_instance ) {
            return array(
                'title' => isset( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '',
                'size'  => isset( $new_instance['size'] ) ? max( 32, min( 256, absint( $new_instance['size'] ) ) ) : 80,
            );
        }
    }
}

add_action( 'widgets_init', function () {
    register_widget( 'WPIDE_About_Author_Widget' );
} );

Notes

The bio comes from Users > Profile > Biographical Info; the photo is the author's Gravatar or avatar.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.