纯代码:wordpress手机注册登录

要实现 WordPress 的手机注册登录功能,通常需要借助插件或者编写自定义代码。以下是一个相对完整的示例,通过自定义代码结合 WordPress 的 API 来实现基本的手机注册登录功能,同时包含前端的 HTML 表单和后端的 PHP 处理逻辑。

创建一个文件,文件名:mobile_reg_login.php把下面的代码粘贴到这个文件里。

<?php
/*
Plugin Name: 手机注册和登录
Description: 启用WordPress的移动注册和登录。
Version: 1.0
Author: 酒醒
*/

// 添加注册表单的短代码
function mobile_registration_form_shortcode() {
    ob_start();
    ?>
    <form id="mobile-registration-form" method="post">
        <input type="text" name="mobile" placeholder="手机号码" required>
        <input type="password" name="password" placeholder="密码" required>
        <input type="submit" value="注册">
    </form>
    <?php
    return ob_get_clean();
}
add_shortcode('mobile_registration_form', 'mobile_registration_form_shortcode');

// 处理注册请求
function handle_mobile_registration() {
    if (isset($_POST['mobile']) && isset($_POST['password'])) {
        $mobile = sanitize_text_field($_POST['mobile']);
        $password = sanitize_text_field($_POST['password']);

        // 生成唯一用户名
        $username = 'user_' . $mobile;

        // 检查用户名是否已存在
        if (username_exists($username)) {
            wp_send_json_error('该手机号码已注册');
        }

        // 创建新用户
        $user_id = wp_create_user($username, $password);
        if (is_wp_error($user_id)) {
            wp_send_json_error($user_id->get_error_message());
        }

        // 更新用户元数据,保存手机号码
        update_user_meta($user_id, 'mobile', $mobile);

        // 自动登录用户
        wp_set_auth_cookie($user_id);
        wp_send_json_success('注册成功');
    }
}
add_action('wp_ajax_nopriv_mobile_registration', 'handle_mobile_registration');
add_action('wp_ajax_mobile_registration', 'handle_mobile_registration');

// 添加登录表单的短代码
function mobile_login_form_shortcode() {
    ob_start();
    ?>
    <form id="mobile-login-form" method="post">
        <input type="text" name="mobile" placeholder="手机号码" required>
        <input type="password" name="password" placeholder="密码" required>
        <input type="submit" value="登录">
    </form>
    <?php
    return ob_get_clean();
}
add_shortcode('mobile_login_form', 'mobile_login_form_shortcode');

// 处理登录请求
function handle_mobile_login() {
    if (isset($_POST['mobile']) && isset($_POST['password'])) {
        $mobile = sanitize_text_field($_POST['mobile']);
        $password = sanitize_text_field($_POST['password']);

        // 生成用户名
        $username = 'user_' . $mobile;

        // 验证用户
        $user = wp_authenticate($username, $password);
        if (is_wp_error($user)) {
            wp_send_json_error('用户名或密码错误');
        }

        // 登录用户
        wp_set_auth_cookie($user->ID);
        wp_send_json_success('登录成功');
    }
}
add_action('wp_ajax_nopriv_mobile_login', 'handle_mobile_login');
add_action('wp_ajax_mobile_login', 'handle_mobile_login');

// 前端 JavaScript 代码,用于处理表单提交
function enqueue_mobile_reg_login_scripts() {
    wp_enqueue_script('jquery');
    $script = "
    jQuery(document).ready(function($) {
        $('#mobile-registration-form').on('submit', function(e) {
            e.preventDefault();
            $.ajax({
                url: '" . admin_url('admin-ajax.php') . "',
                type: 'POST',
                data: {
                    action: 'mobile_registration',
                    mobile: $(this).find('input[name=mobile]').val(),
                    password: $(this).find('input[name=password]').val()
                },
                success: function(response) {
                    if (response.success) {
                        alert(response.data);
                        window.location.reload();
                    } else {
                        alert(response.data);
                    }
                },
                error: function() {
                    alert('请求出错');
                }
            });
        });

        $('#mobile-login-form').on('submit', function(e) {
            e.preventDefault();
            $.ajax({
                url: '" . admin_url('admin-ajax.php') . "',
                type: 'POST',
                data: {
                    action: 'mobile_login',
                    mobile: $(this).find('input[name=mobile]').val(),
                    password: $(this).find('input[name=password]').val()
                },
                success: function(response) {
                    if (response.success) {
                        alert(response.data);
                        window.location.reload();
                    } else {
                        alert(response.data);
                    }
                },
                error: function() {
                    alert('请求出错');
                }
            });
        });
    });
    ";
    wp_add_inline_script('jquery', $script);
}
add_action('wp_enqueue_scripts', 'enqueue_mobile_reg_login_scripts');    

使用说明

  1. 将上述代码保存为 mobile_reg_login.php 文件。
  2. 把该文件上传到 WordPress 插件目录(wp-content/plugins/)。
  3. 登录 WordPress 后台,激活 “Mobile Registration and Login” 插件。
  4. 在需要显示注册表单的页面或文章中使用 [mobile_registration_form] 短代码,在需要显示登录表单的页面或文章中使用 [mobile_login_form] 短代码。

这个示例只是一个基础的实现,实际应用中可能需要考虑更多的安全性和功能完善,例如短信验证码验证、密码加密等。

提示:仅供参考