简单的 WordPress 自助注销功能的示例代码

admin
admin
管理员
532
文章
0
粉丝
综合评论114字数 216阅读0分43秒阅读模式

简单的 WordPress 自助注销功能的示例代码,您可以将其添加到您的主题的 functions.php 文件中或创建一个插件来实现:

<?php
function custom_user_self_unregister() {
    if ( isset( $_GET['action'] ) && $_GET['action'] == 'unregister' ) {
        if ( is_user_logged_in() ) {
            $user_id = get_current_user_id();
            wp_delete_user( $user_id );
            wp_redirect( home_url() );
            exit;
        }
    }
}
add_action( 'init', 'custom_user_self_unregister' );

function custom_add_unregister_link() {
    if ( is_user_logged_in() ) {
        echo '<a href="?action=unregister">注销账号</a>';
    }
}
add_action( 'wp_footer', 'custom_add_unregister_link' );
?>
上述代码首先创建了一个 custom_user_self_unregister 函数,用于处理注销请求。当用户访问带有特定参数 action=unregister 的页面并且已登录时,将删除当前用户并重定向到首页。

 文章源自小武站1z345.cn小武站-https://50-0.cn/856.html

然后,custom_add_unregister_link 函数用于在页面底部添加一个注销链接,仅当用户已登录时显示。
文章源自小武站1z345.cn小武站-https://50-0.cn/856.html文章源自小武站1z345.cn小武站-https://50-0.cn/856.html
  • 本文由 admin 发表于2024年8月17日 13:58:49
  • 转载请务必保留本文链接:https://50-0.cn/856.html
匿名

发表评论

匿名网友
确定