国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

如何利用WordPress創建自定義注冊表單插件

2019-11-15 01:51:35
字體:
來源:轉載
供稿:網友
如何利用WordPRess創建自定義注冊表單插件

來源:http://www.ido321.com/1031.html

原文:Creating a Custom WordPress Registration Form Plugin

譯文:創建一個定制的WordPress插件注冊表單

譯者:dwqs

開門見山,WordPress提供了一個自定義的注冊表單供新用戶使用,或者當添加一個新用戶到已經存在的WordPress站點。但是如果你想實現一個自定義的注冊表單而沒有顯示WordPress儀表盤的選項呢?

在這篇文章中,我們將學會怎么使用標簽模板和短代碼模板的聯合體在WordPress中創建一個自定義的注冊表單。

WordPress默認的注冊表單僅由兩個字段組成—-用戶名和郵箱。

這個僅有的用戶名和郵箱表單字段使得注冊速度非常的簡單。首先,你輸入一個用戶名,然后輸入郵箱,這個郵箱就是用來接收密碼的。接下來,你使用郵箱接收到的密碼登陸站點,并且完成個人資料,把密碼修改成簡單易記得。

僅僅是在站點注冊,而不是讓用戶區經歷這些壓力,那為什么除了用戶名和郵箱之外,不提供一個直接的、包含一些額外重要的表單字段,例如密碼、個人的URL、個人簡介、昵稱和他們的姓名的注冊表單供用戶使用呢?

這對于像Tuts+的多用戶網站是非常有用的。

在這篇文章中,我們將使用下列的表單字段建立一個自定義的表單注冊插件:

  • username
  • password
  • email
  • website URL
  • first name
  • last name
  • nickname
  • biography (or an about section)

這個自定義表單插件可以通過使用短代碼和聯系模板整合到WordPress中。

利用短代碼模板,你可以在你的站點中創建一個正式的注冊頁面。你也可以再一篇發表的文章中是用短代碼模板,這樣用戶就可以在閱讀完你的文章之后進行注冊。

如果你想添加一個注冊表單在你網站側邊欄的某個具體位置,你可以對WordPress主題中僅僅期望放置標簽模板的位置進行編輯,來創建需要的注冊表單。

在創建之前,需要注意的是,用戶名、密碼和電子郵件字段是必需的。

當我們編寫驗證函數時,我們將強制執行這些規則。

構建插件

正如說的那樣,我們開始對插件編碼。首先,包含插件的頭部:

<?php/*  Plugin Name: Custom Registration  Plugin URI: http://code.tutsplus.com  Description: Updates user rating based on number of posts.  Version: 1.0  Author: Agbonghama Collins  Author URI: http://tech4sky.com */

接下來,我們創建一個包含注冊表單的HTML代碼的PHP函數:

function registration_form( $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio ) {    echo '    <style>    div {        margin-bottom:2px;    }         input{        margin-bottom:4px;    }    </style>    ';     echo '    <form action="' . $_SERVER['REQUEST_URI'] . '" method="post">    <div>    <label for="username">Username <strong>*</strong></label>    <input type="text" name="username" value="' . ( isset( $_POST['username'] ) ? $username : null ) . '">    </div>         <div>    <label for="password">Password <strong>*</strong></label>    <input type="password" name="password" value="' . ( isset( $_POST['password'] ) ? $password : null ) . '">    </div>         <div>    <label for="email">Email <strong>*</strong></label>    <input type="text" name="email" value="' . ( isset( $_POST['email']) ? $email : null ) . '">    </div>         <div>    <label for="website">Website</label>    <input type="text" name="website" value="' . ( isset( $_POST['website']) ? $website : null ) . '">    </div>         <div>    <label for="firstname">First Name</label>    <input type="text" name="fname" value="' . ( isset( $_POST['fname']) ? $first_name : null ) . '">    </div>         <div>    <label for="website">Last Name</label>    <input type="text" name="lname" value="' . ( isset( $_POST['lname']) ? $last_name : null ) . '">    </div>         <div>    <label for="nickname">Nickname</label>    <input type="text" name="nickname" value="' . ( isset( $_POST['nickname']) ? $nickname : null ) . '">    </div>         <div>    <label for="bio">About / Bio</label>    <textarea name="bio">' . ( isset( $_POST['bio']) ? $bio : null ) . '</textarea>    </div>    <input type="submit" name="submit" value="Register"/>    </form>    ';}

請注意注冊字段是作為變量傳遞給上面的函數。在函數中,你會看到下面代碼的示例:

( isset( $_POST['lname'] ) ? $last_name : null )

這個三元操作符是檢查全局變量數組$_POST是否包含數據,如果有數據,就把填充的表單字段值保存以便進入下一個字段。

除非你驗證了表單數據并且清空了表單數據,一個注冊表單才能算完成,否則就不算。因此,我們要創建一個名為registration_validation的表單驗證函數。

為了簡化驗證的”痛苦”,我們可以使用WordPress中的 WP_Error 類。跟著我編寫驗證函數:

1、創建函數,并將注冊表單的字段值作為函數的參數傳遞進來

function registration_validation( $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio )  {

2、實例化WP_Error 類,并把實例作為全局變量,以便于我們可以再函數的作用域之外使用。

global $reg_errors;$reg_errors = new WP_Error;

3、記住:我們說的用戶名、密碼和電子郵件是必填的,不要忽略了。為了執行這個規則,我們需要檢查它們中任何一個是否為空。如果為空,我們就將錯誤信息追加給WP_Error 類的實例。

if ( empty( $username ) || empty( $password ) || empty( $email ) ) {    $reg_errors->add('field', 'Required form field is missing');}

4、我們也可以確保用戶名的字符個數不小于4

if ( 4 > strlen( $username ) ) {    $reg_errors->add( 'username_length', 'Username too short. At least 4 characters is required' );}

5、檢查用戶名是否被注冊了

if ( username_exists( $username ) )    $reg_errors->add('user_name', 'Sorry, that username already exists!');

6、利用WordPress的validate_username函數確保用戶名是可用的

if ( ! validate_username( $username ) ) {    $reg_errors->add( 'username_invalid', 'Sorry, the username you entered is not valid' );}

7、確保用戶輸入的密碼的字符個數不小于5

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

8、檢查電子郵件是否有效

if ( !is_email( $email ) ) {    $reg_errors->add( 'email_invalid', 'Email is not valid' );}
9、檢查電子郵件是否被注冊
if ( !is_email( $email ) ) {    $reg_errors->add( 'email_invalid', 'Email is not valid' );}

10.、如果用戶填寫了網站字段,需要檢查其是否有效
if ( ! empty( $website ) ) {    if ( ! filter_var( $website, FILTER_VALIDATE_URL ) ) {        $reg_errors->add( 'website', 'Website is not a valid URL' );    }}

11、最后,我們在WP_Error實例中對錯誤進行循環,并顯示個別的錯誤
if ( is_wp_error( $reg_errors ) ) {     foreach ( $reg_errors->get_error_messages() as $error ) {             echo '<div>';        echo '<strong>ERROR</strong>:';        echo $error . '<br/>';        echo '</div>';             } }

這樣,驗證函數就完成了。接下來是complete_registration()函數,用于處理用戶注冊。用戶的注冊真正完成是通過wp_insert_user函數,用戶的數據作為數據保存后可以作為此函數的參數。
function complete_registration() {    global $reg_errors, $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio;    if ( 1 > count( $reg_errors->get_error_messages() ) ) {        $userdata = array(        'user_login'    =>   $username,        'user_email'    =>   $email,        'user_pass'     =>   $password,        'user_url'      =>   $website,        'first_name'    =>   $first_name,        'last_name'     =>   $last_name,        'nickname'      =>   $nickname,        'description'   =>   $bio,        );        $user = wp_insert_user( $userdata );        echo 'Registration complete. Goto <a href="' . get_site_url() . '/wp-login.php">login page</a>.';      }}

在上面的函數中,我們將$reg_errors作為WP_Error的實例,并將表單字段作為全局變量以便于可以再全局作用域中使用。

我們需要檢查$reg_errors是否包含任何錯誤,如果沒有錯誤,則將用戶注冊信息插入到WordPress的數據庫并用登陸鏈接來顯示注冊完成的信息。

然后,把所有我們之前創建的函數全部放在全局函數custom_registration_function()之中

function custom_registration_function() {    if ( isset($_POST['submit'] ) ) {        registration_validation(        $_POST['username'],        $_POST['password'],        $_POST['email'],        $_POST['website'],        $_POST['fname'],        $_POST['lname'],        $_POST['nickname'],        $_POST['bio']        );                 // sanitize user form input        global $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio;        $username   =   sanitize_user( $_POST['username'] );        $password   =   esc_attr( $_POST['password'] );        $email      =   sanitize_email( $_POST['email'] );        $website    =   esc_url( $_POST['website'] );        $first_name =   sanitize_text_field( $_POST['fname'] );        $last_name  =   sanitize_text_field( $_POST['lname'] );        $nickname   =   sanitize_text_field( $_POST['nickname'] );        $bio        =   esc_textarea( $_POST['bio'] );         // c
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂温| 庄浪县| 泉州市| 织金县| 乐陵市| 清河县| 确山县| 翁牛特旗| 沭阳县| 阿鲁科尔沁旗| 应用必备| 曲阜市| 眉山市| 江门市| 太白县| 武隆县| 长岭县| 彝良县| 青川县| 聊城市| 崇州市| 郁南县| 留坝县| 神木县| 大方县| 衡东县| 罗田县| 仁化县| 玉门市| 新津县| 原平市| 山阳县| 明溪县| 大石桥市| 景德镇市| 威宁| 安庆市| 隆子县| 股票| 金阳县| 桂阳县|