Thursday, October 18, 2012

To create a custom register page for wordpress

<?php
/*
Template name:Register Template
*/

if(isset($_REQUEST['hdnSubmit_frm']))
{
      $username=$_REQUEST['username'];
     
               $first_name=$_REQUEST['first_name'];
               $last_name=$_REQUEST['last_name'];
               $nickname=$_REQUEST['nickname'];
               $billing_company=$_REQUEST['billing_company'];
               $billing_address_1=$_REQUEST['billing_address_1'];
               $billing_address_2=$_REQUEST['billing_address_2'];
               $billing_city=$_REQUEST['billing_city'];
               $billing_postcode=$_REQUEST['billing_postcode'];
               $billing_country=$_REQUEST['billing_country'];
               $billing_state=$_REQUEST['billing_state'];
               /*$billing_email=$_REQUEST['billing_email'];*/
               $billing_email=$useremail;
               $billing_phone=$_REQUEST['billing_phone'];
               $billing_taxid=$taxid;
               /*$billing_taxid=$_REQUEST['billing_taxid'];*/
              
               /*Shipping id*/
              
                $shipping_first_name=$_REQUEST['shipping_first_name'];
                $shipping_last_name=$_REQUEST['shipping_last_name'];
                $shipping_company=$_REQUEST['shipping_company'];
                $shipping_address_1=$_REQUEST['shipping_address_1'];
                $shipping_address_2=$_REQUEST['shipping_address_2'];
                $shipping_city=$_REQUEST['shipping_city'];
                $shipping_postcode=$_REQUEST['shipping_postcode'];
                /*$shipping_taxid=$_REQUEST['shipping_taxid'];*/
                $shipping_taxid=$taxid;
                $shipping_country=$_REQUEST['shipping_country'];
                $shipping_state=$_REQUEST['shipping_state'];
     
      $useremail=$_REQUEST['useremail'];
      $taxid=$_REQUEST['taxid'];
      $password =$_REQUEST['pass'];

      if (username_exists( $username ))
         {
            $msg="<p style='color:red'>
Username already exists</p>
";
}
elseif(email_exists($useremail))
{
$msg="<p style='color:red'>
Email already registered</p>
";

}
else
{
//$detail=mysql_query("select * from wp_users where user_login='$username' and user_email='$useremail' ");
//$detail1=mysql_num_rows($detail);
//if($detail1>0)
//{
// echo "Already registered";
//} 

$user_id = username_exists( $username );
if ( !$user_id and email_exists($useremail) == false ) {
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
$user_id = wp_create_user( $username, $password, $useremail );
} else {
$random_password = __('User already exists.  Password inherited.');
}

update_user_meta($user_id,'first_name',$first_name);
update_user_meta($user_id,'last_name',$last_name);
add_user_meta($user_id,'nickname',$nickname);
add_user_meta($user_id,'billing_company',$billing_company);
add_user_meta($user_id,'billing_address_1',$billing_address_1);
add_user_meta($user_id,'billing_address_2',$billing_address_2);
add_user_meta($user_id,'billing_city',$billing_city);
add_user_meta($user_id,'billing_postcode',$billing_postcode);
add_user_meta($user_id,'billing_country',$billing_country);
add_user_meta($user_id,'billing_state',$billing_state);
add_user_meta($user_id,'billing_email',$billing_email);
add_user_meta($user_id,'billing_phone',$billing_phone);
add_user_meta($user_id,'taxid',$taxid);

add_user_meta($user_id,'shipping_first_name',$shipping_first_name);
add_user_meta($user_id,'shipping_last_name',$shipping_last_name);
add_user_meta($user_id,'shipping_company',$shipping_company);
add_user_meta($user_id,'shipping_address_1',$shipping_address_1);
add_user_meta($user_id,'shipping_address_2',$shipping_address_2);
add_user_meta($user_id,'shipping_city',$shipping_city);
add_user_meta($user_id,'shipping_postcode',$shipping_postcode);
add_user_meta($user_id,'shipping_taxid',$shipping_taxid);
add_user_meta($user_id,'shipping_country',$shipping_country);
add_user_meta($user_id,'shipping_state',$shipping_state);

$msg="<p style='color:green'>
Registered Successfully</p>
";
if($user_id>0)
{
wp_set_auth_cookie( $user_id, false, is_ssl() );
$url=get_option('siteurl');
header("location:$url");
}
}

}
get_header();
get_sidebar();
?>
<script>
function showSelected(){

    if(document.frm.username.value=="")
    {
        alert("Enter Full Name.");
        document.frm.username.focus();
        return false;
    }

    if(document.frm.first_name.value=="")
    {
        alert("Enter First Name.");
        document.frm.first_name.focus();
        return false;
    }
   
    if(document.frm.last_name.value=="")
    {
        alert("Enter Last Name.");
        document.frm.last_name.focus();
        return false;
    }
   
    /*if(document.frm.nickname.value=="")
    {
        alert("Enter Nick Name.");
        document.frm.nickname.focus();
        return false;
    }*/
   
    if(document.frm.billing_company.value=="")
    {
        alert("Enter Billing Company.");
        document.frm.billing_company.focus();
        return false;
    }
   
    if(document.frm.billing_address_1.value=="")
    {
        alert("Enter Billing Address 1.");
        document.frm.billing_address_1.focus();
        return false;
    }
   
    /*if(document.frm.billing_address_2.value=="")
    {
        alert("Enter Billing Address 2.");
        document.frm.billing_address_2.focus();
        return false;
    }*/
   
    if(document.frm.billing_city.value=="")
    {
        alert("Enter Billing City.");
        document.frm.billing_city.focus();
        return false;
    }
   
    if(document.frm.billing_postcode.value=="")
    {
        alert("Enter Billing Post Code.");
        document.frm.billing_postcode.focus();
        return false;
    }
   
    if(document.frm.billing_country.value=="")
    {
        alert("Enter Billing Country.");
        document.frm.billing_country.focus();
        return false;
    }
   
    if(document.frm.billing_state.value=="")
    {
        alert("Enter Billing State.");
        document.frm.billing_state.focus();
        return false;
    }
   
    if(document.frm.billing_phone.value=="")
    {
        alert("Enter Billing Phone.");
        document.frm.billing_phone.focus();
        return false;
    }
   
   
    /* shipping */
   
    if(document.frm.shipping_first_name.value=="")
    {
        alert("Enter Shipping first name.");
        document.frm.shipping_first_name.focus();
        return false;
    }
   
    if(document.frm.shipping_last_name.value=="")
    {
        alert("Enter Shipping Last name.");
        document.frm.shipping_last_name.focus();
        return false;
    }
   
    if(document.frm.shipping_company.value=="")
    {
        alert("Enter Shipping Last name.");
        document.frm.shipping_company.focus();
        return false;
    }
   
    if(document.frm.shipping_address_1.value=="")
    {
        alert("Enter Shipping Address.");
        document.frm.shipping_address_1.focus();
        return false;
    }
   
    if(document.frm.shipping_city.value=="")
    {
        alert("Enter Shipping City.");
        document.frm.shipping_city.focus();
        return false;
    }
   
    if(document.frm.shipping_postcode.value=="")
    {
        alert("Enter Shipping Post code.");
        document.frm.shipping_postcode.focus();
        return false;
    }
   
    if(document.frm.shipping_country.value=="")
    {
        alert("Enter Shipping Country.");
        document.frm.shipping_country.focus();
        return false;
    }
   
    if(document.frm.shipping_state.value=="")
    {
        alert("Enter Shipping state.");
        document.frm.shipping_state.focus();
        return false;
    }
   
   
   
    /* shipping*/
   
   
   
   
    if(document.frm.useremail.value == "")
    {
        alert("Enter Email Id.");
        document.frm.useremail.focus();
        return false;
    }
   
   


    var str=document.frm.useremail.value;
    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
   
    if (str.indexOf(at)==-1)
    {
        alert("Enter valid email id.");
        document.frm.useremail.focus();
        return false;
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
    {
        alert("Enter valid email id.");
        document.frm.useremail.focus();
        return false;
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
    {

        alert("Enter valid email id.");
        document.frm.useremail.focus();
        return false;
    }

    if (str.indexOf(at,(lat+1))!=-1)
    {
        alert("Enter valid email id.");
        document.frm.useremail.focus();
        return false;
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
    {
        alert("Enter valid email id.");
        document.frm.useremail.focus();
        return false;
    }

    if (str.indexOf(dot,(lat+2))==-1)
    {
        alert("Enter valid email id.");
        document.frm.useremail.focus();
        return false;
    }

    if (str.indexOf(" ")!=-1)
    {
        alert("Enter valid email id.");
        document.frm.useremail.focus();
        return false;
    }
   
    if(document.frm.userreemail.value=="")
    {
        alert("Enter retype email.");
        document.frm.userreemail.focus();
        return false;

    }
   
    if(document.frm.useremail.value != document.frm.userreemail.value)
    {
        alert("Email does not match.");
        document.frm.userreemail.focus();
        return false;
    }

    if(document.frm.pass.value=="")
    {
        alert("Enter password.");
        document.frm.pass.focus();
        return false;
    }

    if(document.frm.pass.value.length<6)
    {
        alert("Your password must be at least 6 characters long.");
        document.frm.pass.focus();
        return false;
    }
    if(document.frm.repass.value=="")
    {
        alert("Enter retype password.");
        document.frm.repass.focus();
        return false;

    }
    if(document.frm.pass.value != document.frm.repass.value)
    {
        alert("Password does not match.");
        //document.frm.pass.value = "";
        document.frm.repass.value = "";
        document.frm.pass.focus();
        return false;
    }
    document.frm.submit();
}
</script>
<div class="c_right">
      <div class="c_heading">
Register</div>
<div class="register">
       
        <strong>Email Address &amp; Password</strong>
Enter your email address, which will serve as the Username for your account, and create a password.

Your password must be at least 6 characters with at least 1 digit and at least 1 letter.
       
      </div>
<div class="register1">
         <?php if(isset($_REQUEST['msg'])){ ?>
            <span id="message" style="font-weight: bold;color: red;">Sorry, that email address already has an account, please try another</span>
            <?php } ?>
        <form name="frm" method="post" action="">
        <input type="hidden" id="hdnSubmit_frm" name="hdnSubmit_frm" value="insert" />
       
            <table style="width:100% !important">
<tr>
                <td colspan="2"><strong> Required field *</strong>  <?php if(isset($_REQUEST['hdnSubmit_frm']))
                             {
                               echo $msg;                           
                             } ?></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">Name*</td>
              <td>
                <input name="username" id="username" type="text" value="<?php echo $_REQUEST['username']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">Email Address*</td>
              <td><input name="useremail" id="useremail" type="text" value="<?php echo $_REQUEST['useremail']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">Re-enter Email Address*</td>
              <td>
                    <input name="userreemail" id="userreemail" type="text" value="<?php echo $_REQUEST['useremail']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">Tax Id*</td>
              <td>
                  <input name="taxid" id="taxid" type="text" value="<?php echo $_REQUEST['taxid']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">Password*</td>
              <td><input name="pass" id="pass" type="password" value="<?php echo $_REQUEST['pass']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">Re-enter Password*</td>
              <td>
                  <input name="repass" id="repass" type="password" value="<?php echo $_REQUEST['pass']; ?>" class="billing_fild"></td>
            </tr>
<!-- ----- Billing Address ----------------------------------- -->
<tr style="background:#E9E9E9;"><td colspan="2" ><h2 style="padding-left: 10px;">
Billing Address</h2>
</td></tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">First Name*</td>
              <td>
                  <input name="first_name" id="first_name" type="text" value="<?php echo $_REQUEST['first_name']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">Last Name*</td>
              <td>
                  <input name="last_name" id="last_name" type="text" value="<?php echo $_REQUEST['last_name']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;"> Company*</td>
              <td>
                  <input name="billing_company" id="billing_company" type="text" value="<?php echo $_REQUEST['billing_company']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;"> Address 1*</td>
              <td>
                  <input name="billing_address_1" id="billing_address_1" type="text" value="<?php echo $_REQUEST['billing_address_1']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;"> Address 2</td>
              <td>
                  <input name="billing_address_2" id="billing_address_2" type="text" value="<?php echo $_REQUEST['billing_address_2']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">City*</td>
              <td>
                  <input name="billing_city" id="billing_city" type="text" value="<?php echo $_REQUEST['billing_city']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;"> Post Code*</td>
              <td>
                  <input name="billing_postcode" id="billing_postcode" type="text" value="<?php echo $_REQUEST['billing_postcode']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;"> Country*</td>
              <td>
                  <input name="billing_country" id="billing_country" type="text" value="<?php echo $_REQUEST['billing_country']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;"> State*</td>
              <td>
                  <input name="billing_state" id="billing_state" type="text" value="<?php echo $_REQUEST['billing_state']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">Phone*</td>
              <td>
                  <input name=" billing_phone" id="billing_phone" type="text" value="<?php echo $_REQUEST[' billing_phone']; ?>" class="billing_fild"></td>
              </tr>
<!-- ----- fealds ------------------------------------------------------ -->

           
           
             
             
              <!-- ----- Shipping  Address ----------------------------------- -->
<tr style="background:#E9E9E9;"><td colspan="2" ><h2 style="padding-left: 10px;">
 Shipping Address</h2>
</td></tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;"> First Name*</td>
              <td>
                  <input name="shipping_first_name" id="shipping_first_name" type="text" value="<?php echo $_REQUEST['shipping_first_name']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">Last Name*</td>
              <td>
                  <input name="shipping_last_name" id="shipping_last_name" type="text" value="<?php echo $_REQUEST['shipping_last_name']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">Company*</td>
              <td>
                  <input name="shipping_company" id="shipping_company" type="text" value="<?php echo $_REQUEST['shipping_company']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;"> Address 1*</td>
              <td>
                  <input name="shipping_address_1" id="shipping_address_1" type="text" value="<?php echo $_REQUEST['shipping_address_1']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;"> Address 2</td>
              <td>
                  <input name="shipping_address_2" id="shipping_address_2" type="text" value="<?php echo $_REQUEST['shipping_address_2']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;"> City*</td>
              <td>
                  <input name="shipping_city" id="shipping_city" type="text" value="<?php echo $_REQUEST['shipping_city']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">Post Code*</td>
              <td>
                  <input name="shipping_postcode" id="shipping_postcode" type="text" value="<?php echo $_REQUEST['shipping_postcode']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;"> Country*</td>
              <td>
                  <input name="shipping_country" id="shipping_country" type="text" value="<?php echo $_REQUEST['shipping_country']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td style="background:#F5F5F5;padding-left: 10px;">State*</td>
              <td>
                  <input name="shipping_state" id="shipping_state" type="text" value="<?php echo $_REQUEST['shipping_state']; ?>" class="billing_fild"></td>
              </tr>
<tr>
              <td>&nbsp; </td>
              <td><a href="#submit" onClick="showSelected();"> <img src="<?php bloginfo('template_directory'); ?>/images/register.png" alt="" border="0"></a></td>
              </tr>
</table>
</form>
</div>
</div>
<?php get_footer(); ?>

3 comments: