使用extern表单创建新用户时,WP用户角色保持为空

时间:2017-12-01 11:02:01

标签: php wordpress user-roles

我正在尝试使用php文件中的extern表单创建新的Wordpress用户。 Leerling是学生的荷兰人,而leerling是我使用用户角色插件创建的角色。表单生成的所有用户都需要具有leerling角色。

我使用以下代码尝试这样做:

        <?php
        // Maakt een connectie met de database

        $con = mysqli_connect("localhost", "root", "", "scoreplugin_db");

        if (!$con) {
            die("Connection failed: " . mysqli_connect_error());
        }

        // Haalt gegevens uit het registratieformulier, en genereert overige gegevens

        if (isset($_POST['btn-signup'])) {
            $uname = mysqli_real_escape_string($con, $_POST['uname']);
            $achternaam = mysqli_real_escape_string($con, $_POST['achternaam']);
            $upass = wp_hash_password(mysqli_real_escape_string($con, $_POST['pass']));
            $datum = mysqli_real_escape_string($con, $_POST['datum']);
            $email = mysqli_real_escape_string($con, $_POST['email']);
            $basisschool = mysqli_real_escape_string($con, $_POST['basisschool']);
            $opleidingsduur = mysqli_real_escape_string($con, $_POST['opleidingsduur']);
            $bsgroep = mysqli_real_escape_string($con, $_POST['bsgroep']);
            $url = '#leerling';
            $fullname = $uname . ' ' . $achternaam;


            // Query's om alle user data in de database op te slaan

            $userdata = "INSERT INTO wp_users (user_login,user_pass,user_nicename,user_email,user_url,user_registered,display_name) VALUES('$email','$upass','$uname','$email','$url','now()','$fullname')";

            $userinfo = "INSERT INTO wp_users_info (user_voornaam,user_achternaam,user_geboortedatum,user_basisschool,user_opleidingsduur,user_groep) VALUES('$uname','$achternaam','$datum','$basisschool','$opleidingsduur','$bsgroep')";

            // Voert bovenstaande query's uit

            if (!mysqli_query($con, $userdata)) {
                die('Error: ' . mysqli_error($con));
            }
            if (!mysqli_query($con, $userinfo)) {
                die('Error: ' . mysqli_error($con));
            }

            // User ID ophalen om meta op te slaan
            $newuserid = mysqli_insert_id($con);

            echo "$newuserid";

            // De rank die de gebruiker automatisch meekrijgt
            $rank = 'a:1:{s:8:"leerling";b:1;}';


            // Query's om usermeta op te slaan
            $addusernickname = "INSERT INTO wp_usermeta (user_id,meta_key,meta_value) VALUES ('$newuserid','nickname','$uname')";

            $adduserfirstname = "INSERT INTO wp_usermeta (user_id,meta_key,meta_value) VALUES ('$newuserid','first_name','$uname')";

            $adduserlastname = "INSERT INTO wp_usermeta (user_id,meta_key,meta_value) VALUES ('$newuserid','last_name','$achternaam')";

            $adduserrank = "INSERT INTO wp_usermeta (user_id,meta_key,meta_value) VALUES ('$newuserid','wp_capabilities', '$rank')";

            $adduserlevel = "INSERT INTO wp_usermeta (user_id,meta_key,meta_value) VALUES ('$newuserid','wp_user_level', '0')";

            $adminbar = "INSERT INTO wp_usermeta (user_id,meta_key,meta_value) VALUES ('$newuserid','show_admin_bar_front', 'false')";

            // Voert de metaquery's uit
            if (!mysqli_query($con, $addusernickname)) {
                die('Error: ' . mysqli_error($con));
            }

            if (!mysqli_query($con, $adduserfirstname)) {
                die('Error: ' . mysqli_error($con));
            }

            if (!mysqli_query($con, $adduserlastname)) {
                die('Error: ' . mysqli_error($con));
            }

            if (!mysqli_query($con, $adduserrank)) {
                die('Error: ' . mysqli_error($con));
            }

             if (!mysqli_query($con, $adduserlevel)) {
                die('Error: ' . mysqli_error($con));
            }

             if (!mysqli_query($con, $adminbar)) {
                die('Error: ' . mysqli_error($con));
            }
        }
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Schools in Music - nieuw account</title>
                <link rel="stylesheet" href="style.css" type="text/css" />
            </head>
            <body style="background-repeat: no-repeat;" background="http://test.schoolsinmusic.nl/wp-content/themes/schoolsinmusic/img/bglogin.png">
                <center>
                    <div id="login-form">
                        <form method="post">
                            <table align="center" width="30%" border="0">
                                <tr>
                                    <td><input class="registrationinput" type="text" name="uname" pattern="^[a-zA-Z ]+$" placeholder="Vul hier je voornaam in" required /></td>
                                </tr>
                                <tr>
                                    <td><input class="registrationinput" type="text" name="achternaam" pattern="^[a-zA-Z ]+$" placeholder="Vul hier je achternaam in" required /></td>
                                </tr>
                                <tr>
                                    <td><input class="registrationinput" type="date" min='1900-01-01' max='2017-01-01' id="datepicker" name="datum" placeholder="Wanneer ben je geboren?" required /></td>
                                </tr>
                                <tr>
                                    <td><input class="registrationinput" type="email" name="email" placeholder="Vul hier je emailadres in" required /></td>
                                </tr>
                                <tr>
                                    <td><input class="registrationinput" type="password" name="pass" placeholder="Vul hier je wachtwoord in" required /></td>
                                </tr>
                                <tr>
                                    <td><input class="registrationinput" type="text" name="basisschool" placeholder="Op welke basisschool zit je?" required /></td>
                                </tr>
                                <tr>
                                    <div id="selectoptions">
                                        <td style="font-family: PoetsenOne-Regular; float: right;">Hoe lang speel je al muziek? <select style="font-family: PoetsenOne-Regular;" name="opleidingsduur"> required />
                                                <option value="-1">Minder dan 1 jaar</option>
                                                <option value="1">1 jaar</option>
                                                <option value="2">2 jaar</option>
                                                <option value="3">3 jaar</option>
                                                <option value="4">4 jaar</option>
                                        </td>
                                </tr>
                                <tr>
                                    <td style="font-family: PoetsenOne-Regular; float: right;">In welke groep zit je? <select style="font-family: PoetsenOne-Regular;" name="bsgroep"> required />
                                            <option value="1">Groep 1</option>
                                            <option value="2">Groep 2</option>
                                            <option value="3">Groep 3</option>
                                            <option value="4">Groep 4</option>
                                            <option value="5">Groep 5</option>
                                            <option value="6">Groep 6</option>
                                            <option value="7">Groep 7</option>
                                            <option value="8">Groep 8</option>
                                    </td>
                                    </div>

                                </tr>
                                <tr>
                                    <td><button class="login-button" style="font-family: PoetsenOne-Regular;" type="submit" name="btn-signup" >Registreren</button></td>
                                </tr>

                            </table>
                        </form>
                    </div>
                </center>
            </body>
        </html>

将信息写入数据库。但在管理员中,用户角色保持为空。 (当我以管理员身份登录wp-admin并查看“用户”页面时,新用户没有任何角色。)

有谁能告诉我我做错了什么?

杰弗里

1 个答案:

答案 0 :(得分:0)