안녕하세요. frontend의 계정 페이지에 탈퇴 기능을 넣으려고 합니다. 능력이 부족해서 구글링해서 끙끙대며 하는데 오류가 있네요.
user 가 탈퇴하기에 체크하고 submit 하면, 그걸 받아서 wp_delete_user() 로 처리하려 합니다. wordpress codex에 나온 예제를 거의 그대로 쓰려하니 안되네요.
$_GET['myaccount'] == 'delete'
로 받아서 hook을 걸려고 하는데요. hook이 안걸리는 것 같네요. functions.php에 넣어도 안되네요.. ㅠ
조언을 구해도 될까요? 감사합니다.
<?php
/* Recheck if user is logged in just to be sure, this should have been done already */
if( !is_user_logged_in() ) {
wp_redirect( home_url() );
exit;
}
//delete user
if ( is_user_logged_in() && $_GET['myaccount'] == 'delete' ) {
add_action( 'init', 'n_remove_user_account' );
}
function n_remove_user_account() {
// Verify that the user intended to take this action.
require_once(ABSPATH.'wp-admin/includes/user.php' );
$current_user = wp_get_current_user();
wp_delete_user( $current_user->ID );
wp_redirect( home_url() );
exit;
}
if( $_SERVER['REQUEST_METHOD'] == 'POST' && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) {
$current_user = wp_get_current_user();
//탈퇴 내용 체크 확인
$pass1 = $_POST['deletecheck'];
if( empty($_POST['deletecheck']) ){
wp_redirect( get_permalink() . '?validation=deletchecknone' );
exit;
} else if( $_POST['deletecheck'] == 'yes' ) {
wp_redirect( get_permalink() . '?myaccount=delete' );
exit;
} else {
wp_redirect( get_permalink() . '?validation=unknown' );
exit;
}
/* We got here, assuming everything went OK */
wp_redirect( get_permalink() . '?updated=true' );
exit;
}
?>
<?php get_header(); ?>
<div class="n_page">
<?php while (have_posts()) : the_post(); ?>
<?php if( !empty( $_GET['validation'] ) ): ?>
<?php if( $_GET['validation'] == 'deletchecknone' ): ?>
<div class="error"><?php _e('<p class="n_login_msg">!! Please check to delete your account.</p>', 'mythmeme'); ?></div>
<?php elseif( $_GET['validation'] == 'unknown' ): ?>
<div class="error"><?php _e('<p class="n_login_msg">!! Unknown Error!</p>', 'mythmeme'); ?></div>
<?php endif; ?>
<?php endif; ?>
<?php $current_user = wp_get_current_user(); ?>
<form method="post" id="n_account_form" action="<?php the_permalink(); ?>">
<p class="form-password">
<label for="deletecheck"><?php _e('Delete My Account', 'mythmeme'); ?> </label>
<input name="deletecheck" type="checkbox" id="deletecheck" class="checkbox" value="yes" />
</p>
<p class="form-submit">
<input name="updateuser" type="submit" id="n_wp_submit" class="submit button" value="<?php _e('Delete My Account', 'mythmeme'); ?>" />
<?php wp_nonce_field( 'update-user3' ); ?>
<input name="honey-name" value="" type="text" style="display:none;"></input>
<input name="action" type="hidden" id="action" value="update-user" />
</p><!-- .form-submit -->
</form><!-- #n_account_form -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php get_footer(); ?>
아 해결되었습니다. hook 쓸 필요없이 직접
wp_delete_user( $current_user->ID );
쓰면 해결되는군요.
감사합니다.