PHP/워드프레스

WordPress에서 사용자 프로필 페이지 URL 커스터마이징하기

이영훈닷컴 2025. 1. 30. 17:01
728x90

1. 기본 제공되는 Author 페이지 활용하기
WordPress는 기본적으로 /author/username 형태의 URL을 제공합니다. 이를 활용하여 사용자의 프로필 페이지를 커스터마이징 할 수 있습니다.

author.php 템플릿 생성 (테마 폴더에 추가)

 

<?php
get_header();
$author_id = get_queried_object_id();
$author = get_userdata($author_id);
?>

<h1><?php echo esc_html($author->display_name); ?>님의 프로필</h1>
<p>이메일: <?php echo esc_html($author->user_email); ?></p>
<p>설명: <?php echo esc_html(get_the_author_meta('description', $author_id)); ?></p>

<h2>최근 게시물</h2>
<ul>
    <?php
    $args = array(
        'author'        => $author_id,
        'post_status'   => 'publish',
        'posts_per_page' => 5,
    );
    $author_posts = new WP_Query($args);

    if ($author_posts->have_posts()) :
        while ($author_posts->have_posts()) : $author_posts->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        endwhile;
    else :
        echo '<li>게시물이 없습니다.</li>';
    endif;
    wp_reset_postdata();
    ?>
</ul>

<?php get_footer(); ?>

 

접속 URL 예시: https://example.com/author/사용자명

 

2. domain.com/user/{ID} 형태로 프로필 페이지 만들기
사용자 ID 기반 URL을 만들려면 rewrite_rules을 활용해야 합니다.

functions.php에 사용자 프로필 URL 리라이트 추가

function custom_user_rewrite_rule() {
    add_rewrite_rule('^user/([0-9]+)/?', 'index.php?pagename=user-profile&userid=$matches[1]', 'top');
}
add_action('init', 'custom_user_rewrite_rule');

function custom_query_vars($vars) {
    $vars[] = 'userid';
    return $vars;
}
add_filter('query_vars', 'custom_query_vars');

 

사용자 프로필 템플릿 생성 (page-user-profile.php)

<?php
/*
Template Name: 사용자 프로필
*/

get_header();

$userid = get_query_var('userid');

if ($userid) {
    $user = get_userdata($userid);

    if ($user) {
        echo '<h1>' . esc_html($user->display_name) . '님의 프로필</h1>';
        echo '<p>이메일: ' . esc_html($user->user_email) . '</p>';
        echo '<p>설명: ' . esc_html(get_the_author_meta('description', $userid)) . '</p>';
    } else {
        echo '<p>사용자를 찾을 수 없습니다.</p>';
    }
} else {
    echo '<p>잘못된 요청입니다.</p>';
}

get_footer();
?>

 

접속 URL 예시: https://example.com/user/2
위 URL에 접속하면 ID가 2인 사용자의 정보를 표시할 수 있습니다.

마지막 단계:
설정을 저장한 후 "퍼머링크 설정"에서 "저장하기" 버튼을 눌러야 새 URL 구조가 적용됩니다.

3. domain.com/user/{username} 형태로 프로필 페이지 만들기
사용자 ID가 아니라 사용자명(username) 기반의 URL을 만들려면 아래와 같이 설정합니다.

functions.php에 사용자명 기반 리라이트 추가

function custom_username_rewrite_rule() {
    add_rewrite_rule('^user/([^/]+)/?', 'index.php?pagename=user-profile&username=$matches[1]', 'top');
}
add_action('init', 'custom_username_rewrite_rule');

function custom_query_vars($vars) {
    $vars[] = 'username';
    return $vars;
}
add_filter('query_vars', 'custom_query_vars');

 

page-user-profile.php 수정 (사용자명 기반 조회)

<?php
/*
Template Name: 사용자 프로필
*/

get_header();

$username = get_query_var('username');

if ($username) {
    $user = get_user_by('login', $username); // 'slug' 사용 시 닉네임 검색 가능

    if ($user) {
        echo '<h1>' . esc_html($user->display_name) . '님의 프로필</h1>';
        echo '<p>이메일: ' . esc_html($user->user_email) . '</p>';
        echo '<p>설명: ' . esc_html(get_the_author_meta('description', $user->ID)) . '</p>';
    } else {
        echo '<p>사용자를 찾을 수 없습니다.</p>';
    }
} else {
    echo '<p>잘못된 요청입니다.</p>';
}

get_footer();
?>

 

접속 URL 예시: https://example.com/user/johndoe
사용자 johndoe의 프로필을 볼 수 있습니다.

마지막 단계:
퍼머링크 설정에서 "저장하기" 버튼을 눌러야 새로운 URL 구조가 적용됩니다.

 

결론

방법URL 형태설명

기본 제공 /author/username WordPress 내장 기능 (author.php 수정)
사용자 ID 기반 /user/{ID} get_userdata($userid) 사용
사용자명 기반 /user/{username} get_user_by('login', $username) 사용

추천 방법:

  1. 기본 제공 (/author/username): WordPress 내장 기능을 그대로 활용하면 유지보수가 편리함
  2. 사용자명 기반 (/user/{username}): SEO 최적화에 유리함
  3. 사용자 ID 기반 (/user/{ID}): ID 기반으로 빠르게 조회 가능하지만, 직관적이지 않음

위 방법을 적용하면 사용자 ID 또는 사용자명으로 프로필 페이지를 직접 조회할 수 있습니다!

 

퍼머링크 설정에서 "저장하기"를 꼭 눌러야 합니다!

 

배운 점 (TIL)

  1. add_rewrite_rule()을 사용하면 원하는 형태의 사용자 URL을 만들 수 있다.
  2. get_query_var('userid') 또는 get_query_var('username')을 활용해 사용자 정보를 불러올 수 있다.
  3. WordPress의 기본 author.php를 활용하면 손쉽게 프로필 페이지를 커스터마이징 할 수 있다.
  4. 퍼머링크 설정 저장은 필수! (리라이트 규칙을 적용하기 위해)

앞으로 사용자별 대시보드 페이지나 커스텀 기능을 추가할 때도 이 원리를 활용할 수 있을 것 같다!

728x90