來源:http://www.ido321.com/1031.html
原文:Creating a Custom WordPress Registration Form Plugin
譯文:創建一個定制的WordPress插件注冊表單
譯者:dwqs
開門見山,WordPress提供了一個自定義的注冊表單供新用戶使用,或者當添加一個新用戶到已經存在的WordPress站點。但是如果你想實現一個自定義的注冊表單而沒有顯示WordPress儀表盤的選項呢?
在這篇文章中,我們將學會怎麼使用標簽模板和短代碼模板的聯合體在WordPress中創建一個自定義的注冊表單。
WordPress默認的注冊表單僅由兩個字段組成—-用戶名和郵箱。
這個僅有的用戶名和郵箱表單字段使得注冊速度非常的簡單。首先,你輸入一個用戶名,然後輸入郵箱,這個郵箱就是用來接收密碼的。接下來,你使用郵箱接收到的密碼登陸站點,並且完成個人資料,把密碼修改成簡單易記得。
僅僅是在站點注冊,而不是讓用戶區經歷這些壓力,那為什麼除了用戶名和郵箱之外,不提供一個直接的、包含一些額外重要的表單字段,例如密碼、個人的URL、個人簡介、昵稱和他們的姓名的注冊表單供用戶使用呢?
這對於像Tuts+的多用戶網站是非常有用的。
在這篇文章中,我們將使用下列的表單字段建立一個自定義的表單注冊插件:
這個自定義表單插件可以通過使用短代碼和聯系模板整合到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'] );
// call @function complete_registration to create the user
// only when no WP_error is found
complete_registration(
$username,
$password,
$email,
$website,
$first_name,
$last_name,
$nickname,
$bio
);
}
registration_form(
$username,
$password,
$email,
$website,
$first_name,
$last_name,
$nickname,
$bio
);
}
我需要說明一下全局函數custom_registration_function()中有哪些代碼。
首先,我通過檢查$_POST['submit']是否是空來確定表單是否提交。如果提交了,我就調用
registration_validation()函數來驗證用戶提交的表單.
然後,確保表單數據的有效性並將有效的數據在表單字段域之後用一個變量命名。最後,調用
complete_registration()函數保存用戶。我需要調用registration_form()函數來顯示用戶注冊表單。
我之前提到過,我打算用短代碼模板來支持注冊插件。下面就是短代碼模的支持代碼:
// Register a new shortcode: [cr_custom_registration]
add_shortcode( 'cr_custom_registration', 'custom_registration_shortcode' );
// The callback function that will replace [book]
function custom_registration_shortcode() {
ob_start();
custom_registration_function();
return ob_get_clean();
}
到這裡為止,我們已經完成了插件,下面的一張圖片展示了注冊表單的外觀。
注意,你可能不會得到完全相同的外觀,因為WordPress站點的CSS樣式不同。
應用插件
為了在WordPress的文章頁或獨立頁面使用這個插件,可以加入以下代碼:[cr_custom_registration]
也可以添加列出的模板標記<?php custom_registration_function(); ?>.,這樣可以讓表單插件成
為WordPress主題的一個部分。你可以從這篇文章的附加代碼得到這個插件。
總結
在這篇文章中,我們一步步創建了一個自定義注冊表單並添加到WordPress站點。你可以添加額外字段,進一
步擴展這個注冊表單,例如用戶角色,AOL IM 賬戶,但是確保數據時有效的。
如果你有任何問題或建議你,可以留下評論。
下一篇:如何在網站中添加音樂
category-6.php頁面有引入wp_head和wp_footer麼,插件需要的資源文件需要這兩個來加載
這還要插件?!我的wordpress基本上沒用插件,這麼簡單的功能,花個把小時寫一個……
插件越多越慢