Using WordPress ‘get_edit_user_link()’ PHP function

The get_edit_user_link() WordPress PHP function retrieves the edit user link for a specified user.

Usage

get_edit_user_link( $user_id );

Parameters

  • $user_id (int) Optional: User ID. Defaults to the current user. Default: null

More information

See WordPress Developer Resources: get_edit_user_link()

Examples

This code snippet displays the edit user link for the user currently viewing the page.

echo '<a href="' . get_edit_user_link() . '">Edit Profile</a>';

This code snippet retrieves the edit user link for the user with the ID 5.

$user_id = 5;
echo '<a href="' . get_edit_user_link($user_id) . '">Edit User</a>';

This example gets all users with the ‘editor’ role and displays their edit user links.

$user_query = new WP_User_Query( array( 'role' => 'editor' ) );
$editors = $user_query->get_results();

foreach( $editors as $editor ) {
    $userdata = get_userdata( $editor->ID );
    echo '<a href="' . get_edit_user_link( $editor->ID ) . '">' . esc_attr( $userdata->user_nicename ) . '</a>';
}

This example adds an edit user link to the WordPress admin bar.

add_action( 'admin_bar_menu', 'add_edit_user_link_to_admin_bar', 999 );

function add_edit_user_link_to_admin_bar( $wp_admin_bar ) {
    if ( is_user_logged_in() ) {
        $current_user = wp_get_current_user();
        $edit_user_link = get_edit_user_link( $current_user->ID );

        $args = array(
            'id' => 'edit_user_link',
            'title' => 'Edit Profile',
            'href' => $edit_user_link,
            'parent' => 'user-actions',
        );

        $wp_admin_bar->add_node( $args );
    }
}

This example creates a [edit_user_link] shortcode that outputs the edit user link for the current user.

add_shortcode( 'edit_user_link', 'edit_user_link_shortcode' );

function edit_user_link_shortcode() {
    $current_user = wp_get_current_user();
    $edit_user_link = get_edit_user_link( $current_user->ID );

    return '<a href="' . $edit_user_link . '">Edit Profile</a>';
}