Halloween Sale 15% off Ends in See the plans
PHP

Member Dashboard Shortcode

Adds a [wpide_account] shortcode that greets the logged-in member and shows their avatar, membership details, recent posts and comments, and account links.

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

Code

// Label => URL for the links box. Use full URLs or paths like /account/edit/.
$wpide_account_links = array(
    'Edit profile' => admin_url( 'profile.php' ),
);

add_shortcode( 'wpide_account', function () use ( $wpide_account_links ) {
    if ( ! is_user_logged_in() ) {
        return '<p class="wpide-account-login">Please <a href="' . esc_url( wp_login_url( get_permalink() ) ) . '">log in</a> to see your account.</p>';
    }

    $user  = wp_get_current_user();
    $roles = array_map( function ( $role ) {
        return isset( wp_roles()->role_names[ $role ] ) ? translate_user_role( wp_roles()->role_names[ $role ] ) : $role;
    }, $user->roles );

    $html  = '<div class="wpide-account">';
    $html .= '<div class="wpide-account__head" style="display:flex;gap:1em;align-items:center;flex-wrap:wrap">' . get_avatar( $user->ID, 72, '', '' );
    $html .= '<div><h2 style="margin:0">' . esc_html( sprintf( 'Hello, %s', $user->first_name ? $user->first_name : $user->display_name ) ) . '</h2>';
    $html .= '<p style="margin:0">' . esc_html( sprintf( 'Member since %s', wp_date( get_option( 'date_format' ), strtotime( $user->user_registered . ' UTC' ) ) ) ) . ( $roles ? ' · ' . esc_html( implode( ', ', $roles ) ) : '' ) . '</p></div></div>';

    if ( current_user_can( 'edit_posts' ) ) {
        $posts = get_posts( array( 'author' => $user->ID, 'post_status' => array( 'publish', 'draft', 'pending' ), 'numberposts' => 5, 'no_found_rows' => true ) );

        if ( $posts ) {
            $html .= '<h3>Your recent posts</h3><ul>';
            foreach ( $posts as $post ) {
                $html .= '<li><a href="' . esc_url( 'publish' === $post->post_status ? get_permalink( $post ) : get_edit_post_link( $post ) ) . '">' . esc_html( get_the_title( $post ) ) . '</a> <small>(' . esc_html( get_post_status_object( $post->post_status )->label ) . ')</small></li>';
            }
            $html .= '</ul>';
        }
    }

    $comments = get_comments( array( 'user_id' => $user->ID, 'number' => 5, 'status' => 'approve', 'no_found_rows' => true ) );

    if ( $comments ) {
        $html .= '<h3>Your recent comments</h3><ul>';
        foreach ( $comments as $comment ) {
            $html .= '<li><a href="' . esc_url( get_comment_link( $comment ) ) . '">' . esc_html( wp_trim_words( $comment->comment_content, 12 ) ) . '</a></li>';
        }
        $html .= '</ul>';
    }

    $html .= '<h3>Your account</h3><ul class="wpide-account__links">';
    foreach ( $wpide_account_links as $label => $url ) {
        $html .= '<li><a href="' . esc_url( $url ) . '">' . esc_html( $label ) . '</a></li>';
    }
    $html .= '<li><a href="' . esc_url( wp_logout_url( home_url( '/' ) ) ) . '">Log out</a></li></ul></div>';

    return $html;
} );

Notes

Set $wpide_account_links to your own pages (for example a page holding [wpide_edit_profile]); visitors who are not logged in get a login link.

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.