<?php
$user_data = array(
'user_login' => 'testuser',
'user_pass' => 'testpassword123!',
'user_email' => 'testuser@example.com',
'first_name' => 'Test',
'last_name' => 'User',
'role' => 'subscriber' // 역할을 구독자로 지정
);
$user_id = wp_insert_user( $user_data );
// 사용자 생성 성공 여부 확인
if ( is_wp_error( $user_id ) ) {
// 오류 발생 시 오류 메시지 출력
$error_message = $user_id->get_error_message();
echo "사용자 생성 오류: " . $error_message;
} else {
// 성공 시 사용자 ID 출력
echo "사용자가 성공적으로 생성되었습니다. 사용자 ID: " . $user_id;
}
?>
$userdata = array(
'ID' => 0, //(int) User ID. If supplied, the user will be updated.
'user_pass' => '', //(string) The plain-text user password.
'user_login' => '', //(string) The user's login username.
'user_nicename' => '', //(string) The URL-friendly user name.
'user_url' => '', //(string) The user URL.
'user_email' => '', //(string) The user email address.
'display_name' => '', //(string) The user's display name. Default is the user's username.
'nickname' => '', //(string) The user's nickname. Default is the user's username.
'first_name' => '', //(string) The user's first name. For new users, will be used to build the first part of the user's display name if $display_name is not specified.
'last_name' => '', //(string) The user's last name. For new users, will be used to build the second part of the user's display name if $display_name is not specified.
'description' => '', //(string) The user's biographical description.
'rich_editing' => '', //(string|bool) Whether to enable the rich-editor for the user. False if not empty.
'syntax_highlighting' => '', //(string|bool) Whether to enable the rich code editor for the user. False if not empty.
'comment_shortcuts' => '', //(string|bool) Whether to enable comment moderation keyboard shortcuts for the user. Default false.
'admin_color' => '', //(string) Admin color scheme for the user. Default 'fresh'.
'use_ssl' => '', //(bool) Whether the user should always access the admin over https. Default false.
'user_registered' => '', //(string) Date the user registered. Format is 'Y-m-d H:i:s'.
'show_admin_bar_front' => '', //(string|bool) Whether to display the Admin Bar for the user on the site's front end. Default true.
'role' => '', //(string) User's role.
'locale' => '', //(string) User's locale. Default empty.
);
블로그
-
wp_insert_user
-
is_email
if ( is_email( $email_address ) ) { echo "'" . $email_address . "'은(는) 유효한 이메일 주소입니다."; } else { echo "'" . $email_address . "'은(는) 유효하지 않은 이메일 주소입니다."; } -
wp_signon
wp_signon()은 워드프레스에서 사용자 자격증명을 받아 로그인 처리를 하는 함수로, 사용자 로그인 정보(아이디, 비밀번호, ‘remember me’ 설정)를 배열로 전달하면, 성공 시WP_User객체, 실패 시WP_Error객체를 반환하며 로그인 쿠키를 설정해 줍니다. 예제는 사용자 정보를 배열로 만들어wp_signon()에 전달하고, 반환 값으로 로그인 성공 여부를 확인하여 오류 메시지를 출력하거나 사용자 세션을 설정하는 방식입니다.if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) { // POST 데이터에서 로그인 정보 가져오기 $creds = array( 'user_login' => $_POST['username'], // 폼 필드 이름에 맞게 수정 'user_password' => $_POST['password'], // 폼 필드 이름에 맞게 수정 'remember' => isset($_POST['rememberme']) // 'remember me' 체크 여부 ); // wp_signon 함수 호출 $user = wp_signon( $creds, false ); // 두 번째 인자는 'false'로 설정하여 보안 강화 // 로그인 결과 확인 if ( is_wp_error( $user ) ) { echo '로그인 실패: ' . $user->get_error_message(); // 로그인 실패 시 처리 (예: 오류 메시지 표시) } else { // 로그인 성공 시 처리 // $user 객체는 성공한 사용자의 정보를 담고 있습니다. // wp_set_current_user($user->ID); // 세션 설정 (선택 사항, wp_signon이 자동으로 처리하는 경우도 있음) // wp_set_auth_cookie($user->ID); // 쿠키 설정 (선택 사항) echo '로그인 성공! 사용자: ' . $user->display_name; // 성공 후 리디렉션 등 추가 작업 // wp_redirect( home_url() ); // exit; } }// 관리자 사용자 'admin_user'를 자동 로그인 시키는 예시 $creds = array( 'user_login' => 'admin_user', 'user_password' => 'your_secure_password', // 실제 비밀번호로 변경 'remember' => true ); $user = wp_signon( $creds, false ); if ( !is_wp_error( $user ) ) { // 성공 시 echo '자동 로그인 성공!'; // 현재 사용자 설정 및 쿠키 설정 (필요시) // wp_set_current_user($user->ID); // wp_set_auth_cookie($user->ID); } else { // 실패 시 echo '자동 로그인 실패: ' . $user->get_error_message(); } -
wp_authenticate
WordPress에서
wp_authenticate는 사용자 로그인 자격 증명(아이디/이메일 및 비밀번호)을 검증하는 핵심 함수입니다. 이 함수는WP_User객체를 반환하거나, 인증 실패 시WP_Error를 반환합니다.주의:
wp_authenticate()는 단순히 인증만 수행하며, 실제 세션을 생성하고 로그인 처리(쿠키 설정)를 하려면wp_signon()을 사용해야 합니다.