2014-11-05 79 views
1

只是想創建自定義註冊表單和代碼,我爲它做了如下。一切都按照我的計劃進行,但是當我在註冊表單中添加值時,我看不到任何錯誤,也沒有看到插入到數據庫中的任何記錄。任何機構都可以幫助我解決這個問題。如何將數據插入wordpress表(默認)

<?php 

function super_registration(){ 
    if(isset($_POST['submit'])){ 
     /*registration_validation(
     $_POST['uname'], 
     $_POST['pwd'], 
     $_POST['email'], 
     $_POST['weburl'], 
     $_POST['fname'], 
     $_POST['lname'], 
     $_POST['nname'], 
     $_POST['bio'] 
     ); 

     global $uname, $pwd, $email, $weburl, $fname, $lname, $nname, $bio; 
     $uname = sanitize_user($_POST['uname']); 
     $pwd = esc_attr($_POST['pwd']); 
     $email = sanitize_email($_POST['email']); 
     $weburl = esc_url($_POST['weburl']); 
     $fname = sanitize_text_field($_POST['fname']); 
     $lname = sanitize_text_field($_POST['lname']); 
     $nname = sanitize_text_field($_POST['nname']); 
     $bio = esc_textarea($_POST['bio']); 

     final_registration($uname, $pwd, $email, $weburl, $fname, $lname, $nname, $bio);  
    } 
    Registration_form($uname, $pwd, $email, $weburl, $fname, $lname, $nname, $bio);*/ 

    echo "test"; 
    exit; 
    } 
} 

function registration_form($uname, $pwd, $email, $weburl, $fname, $lname, $nname, $bio){ 
    echo ' 
    <style> 
    div { 
    margin-bottom: 2px; 
    } 
    input { 
    margin-bottom: 4px; 
    } 
    </style> 
    '; 

    echo ' 
    <form action="' . $_SERVER['Request_URI'] . '"method = "post"> 

    <div> 
    <label for="uname"> User Name <Strong>*</strong></label> 
    <input type="text" name="uname" value= "' . (isset($_POST['uname']) ? $uname : NULL) .'"> 
    <div> 

    <div> 
    <label for="pwd"> Password <Strong>*</strong></label> 
    <input type="password" name="pwd" values="' . (isset($_POST['pwd']) ? $pwd : NULL) . '"> 
    </div> 

    <div> 
    <label for="email"> Email Address <Strong>*</strong></label> 
    <input type="text" name="email" values="' . (isset($_POST['email']) ? $email : NULL) . '"> 
    </div>  

    <div> 
    <label for="weburl"> Website URL </label> 
    <input type="text" name="weburl" values="' . (isset($_POST['weburl']) ? $weburl : NULL) . '"> 
    </div> 

    <div> 
    <label for="fname"> First Name </label> 
    <input type="text" name="fname" values="' . (isset($_POST['fname']) ? $fname : NULL) . '"> 
    </div> 

    <div> 
    <label for="lname"> Last Name </label> 
    <input type="text" name="lname" values="' . (isset($_POST['lname']) ? $lname : NULL) . '"> 
    </div>  

    <div> 
    <label for="nname"> Nick Name </label> 
    <input type="text" name="nname" values="' . (isset($_POST['nname']) ? $nname : NULL) . '"> 
    </div>  

    <div> 
    <label for="bio">About </label> 
    <textarea name="bio">' . (isset($_POST['bio']) ? $bio : NULL) . '</textarea> 
    </div> 

    <input type="submit" name="Submit" value="register" /> 
    </form> ';   

} 

function registration_validation($uname, $pwd, $email, $weburl, $fname, $lname, $nname, $bio){ 
    global $regerror; 
    $regerror = new WP_Error; 

    if(empty($uname) || empty($pwd) || empty($email)){ 
     $regerror->add('field','Required Field are Missing'); 
    } 

/* if(strlen($uname) < 4){ 
     $regerror->add('uname_length','Enter Username containing more than 4 character'); 
    } 

    if(uname_exits($uname)){ 
     $regerror->add('uname','User name already exits please enter different user name'); 
    } 

    if(! validate_uname($uname)){ 
     $regerror->add('uname_invalid', 'Sorry, the username you entered is not valid'); 
    } 

    if (strlen($pwd) < 5) { 
     $regerror->add('pwd', 'Password length must be greater than 5'); 
    } 

    if (!is_email($email)) { 
     $regerror->add('email_invalid', 'Email is not valid'); 
    } 

    if (email_exists($email)) { 
     $regerror->add('email', 'Email Already in use'); 
    } 

    if (!empty($weburl)) { 
     if (!filter_var($weburl, FILTER_VALIDATE_URL)) { 
      $regerror->add('weburl', 'Website is not a valid URL'); 
     } 
    } 
*/ 
    if (is_wp_error($regerror)) { 

     foreach ($regerror->get_error_messages() as $error) { 
      echo '<div>'; 
      echo '<strong>ERROR</strong>:'; 
      echo $error . '<br/>'; 
      echo '</div>'; 
     } 
    } 
} 

function final_registration(){ 
    global $regerror, $uname, $pwd, $email, $weburl, $fname, $lname, $nname, $bio; 
    if(count($regerror->get_error_messages()) < 1){ 
     $udata = array(
     'user_login' => $uname, 
     'user_pass' => $pwd, 
     'user_email' => $email, 
     'user_url' => $weburl, 
     'first_name' => $fname, 
     'last_name' => $lname, 
     'nickname' => $nname, 
     'description' => $bio, 
); 

    $user = wp_insert_user($udata); 
    echo 'Registration Successfully Done . goto <a href="' .get_site_url() . '/wp-login.php">Login Page</a>.'; 
    } 
} 

add_shortcode('srt_super_registration','super_registration_shortcode'); 
function super_registration_shortcode(){ 
    ob_start(); 
    super_registration(); 
    return ob_get_clean(); 
} 

?> 
+0

? – 2014-11-05 11:17:09

+0

其實我想插入數據到用戶表中。 – 2014-11-05 11:21:16

+0

您可以使用您已經使用過的wp_insert_user函數。你能告訴我它工作還是有什麼問題? – 2014-11-05 11:22:13

回答

0

您的表單標記中存在錯誤「=」is missing.pl隨以下變化。

echo ' 
<form action="' . $_SERVER['Request_URI'] . '" method = "post"> 

而且您在$ UDATA參數是您要添加數據的表錯

$udata = array(
    'user_login'  => $uname, 
    'user_pass' => $pwd, 
    'user_email' => $email, 
    'user_url' => $weburl, 
    'first_name' => $fname, 
    'last_name'  => $lname, 
    'nickname'  => $nname, 
    'description'  => $bio, 
); 
+0

哦,是的,我錯過了「=」在我的表單標籤,但添加後仍然面臨同樣的問題。 – 2014-11-05 11:45:40

+0

pl在if(isset($ _POST ['submit'])){echo「test」;出口;並檢查並讓我知道它是否可以工作 – 2014-11-05 11:54:05

+0

當我在super_registration函數中添加這個函數並評論其他代碼時,我使用了「if(isset($ _POST ['submit'])){」比我什麼都看不到......甚至不是「測試「。 – 2014-11-05 12:00:08