2017-10-11 95 views
0

如何將自定義字段添加到此註冊碼中?例如,我想添加「公司名稱」。在這種形式下,我們只有基本的領域。如何將自定義字段添加到註冊表單中? wordpress

我的代碼:

<div class="registration"> 
      <form name="registerform" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post"> 
       <p> 
        <label for="user_login">Username</label> 
        <input type="text" name="user_login" value=""> 
       </p> 
       <p> 
        <label for="user_email">E-mail</label> 
        <input type="text" name="user_email" id="user_email" value=""> 
       </p> 
       <p style="display:none"> 
        <label for="confirm_email">Please leave this field empty</label> 
        <input type="text" name="confirm_email" id="confirm_email" value=""> 
       </p> 

       <p id="reg_passmail">A password will be e-mailed to you.</p> 

       <input type="hidden" name="redirect_to" value="/login/?action=register&success=1" /> 
       <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" value="Register" /></p> 
      </form> 
     </div> 
+0

我們總是樂於幫助和支持新的編碼器,但是您首先需要幫助自己。 : - )*** [做了更多的研究之後](https://meta.stackoverflow.com/q/261592/1011527)如果你有問題**發佈你已經嘗試了清楚的解釋是什麼是不是'工作**並提供[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。閱讀[如何問](http://stackoverflow.com/help/how-to-ask)一個很好的問題。請務必[參觀](http://stackoverflow.com/tour)並閱讀[this](https://meta.stackoverflow.com/q/347937/1011527)。 – mega6382

回答

0

此代碼將添加一個名爲公司名稱的自定義字段並強制登記。您可以在線關注WordPress customizing registration表單。

function myplugin_add_registration_fields() { 

    //Get and set any values already sent 
    $company_name = (isset($_POST['company_name']) ? $_POST['company_name'] : ''); 
    ?> 

    <p> 
     <label for="company_name"><?php _e('Company Name', 'myplugin_textdomain') ?><br /> 
      <input type="text" name="company_name" id="company_name" class="input" value="<?php echo esc_attr(stripslashes($company_name)); ?>" size="50" /></label> 
    </p> 

    <?php 
} 

add_action('register_form', 'myplugin_add_registration_fields'); 

function myplugin_check_fields($errors, $sanitized_user_login, $user_email) { 

    if (empty ($_POST['company_name'])) { 
     $errors->add('company_name_error', __('<strong>ERROR</strong>: Company name is required.', 'my_textdomain')); 
    } 

    return $errors; 
} 

add_filter('registration_errors', 'myplugin_check_fields', 10, 3); 


function myplugin_registration_save($user_id) { 

    if (isset($_POST['company_name'])) 
     update_user_meta($user_id, 'company_name', $_POST['company_name']); 

} 

add_action('user_register', 'myplugin_registration_save', 10, 1); 
+0

它的工作,但我沒有看到管理面板中的這個字段... –

+0

你的代碼中有些東西是錯誤的,但解決方案是在鏈接「自定義註冊」一切都好!謝謝:) –

+0

將字段添加到管理面板是一個完全不同的工作,在問題中沒有提到。你可以從教程中複製代碼它就在那裏:) –

相關問題