Create registration form in Wordpress without plugin.
<?php
/* Template Name: registration */
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
// End of the loop.
endwhile;
?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php
global $wpdb, $user_ID;
if (!$user_ID) {
//All code goes in here.
}
else {
wp_redirect( home_url() ); exit;
}
?>
<?php
$info = '';
if (isset($_POST['user_registeration']))
{
//registration_validation($_POST['username'], $_POST['useremail']);
global $reg_errors;
$reg_errors = new WP_Error;
$username=$_POST['username'];
$useremail=$_POST['useremail'];
$password=$_POST['password'];
if(empty( $username ) || empty( $useremail ) || empty($password))
{
$reg_errors->add('field', 'Required form field is missing');
}
if ( 6 > strlen( $username ) )
{
$reg_errors->add('username_length', 'Username too short. At least 6 characters is required' );
}
if ( username_exists( $username ) )
{
$reg_errors->add('user_name', 'The username you entered already exists!');
}
if ( ! validate_username( $username ) )
{
$reg_errors->add( 'username_invalid', 'The username you entered is not valid!' );
}
if ( !is_email( $useremail ) )
{
$reg_errors->add( 'email_invalid', 'Email id is not valid!' );
}
if ( email_exists( $useremail ) )
{
$reg_errors->add( 'email', 'Email Already exist!' );
}
if ( 5 > strlen( $password ) ) {
$reg_errors->add( 'password', 'Password length must be greater than 5!' );
}
if (is_wp_error( $reg_errors ))
{
foreach ( $reg_errors->get_error_messages() as $error )
{
$signUpError='<p style="color:#FF0000; text-aling:left;"><strong>ERROR</strong>: '.$error . '<br /></p>';
}
}
if ( 1 > count( $reg_errors->get_error_messages() ) )
{
// sanitize user form input
global $username, $useremail;
$username = sanitize_user( $_POST['username'] );
$useremail = sanitize_email( $_POST['useremail'] );
$password = esc_attr( $_POST['password'] );
$userdata = array(
'user_login' => $username,
'user_email' => $useremail,
'user_pass' => $password
);
$user = wp_insert_user( $userdata );
if($user){
$info = 'You have been registered successfully';
}
}
}
?>
<?php echo $info; ?>
<h3>Create your account</h3>
<form action="" method="post" name="user_registeration">
<label>Username <span class="error">*</span></label>
<input type="text" name="username" placeholder="Enter Your Username" class="text" required /><br />
<label>Email address <span class="error">*</span></label>
<input type="text" name="useremail" class="text" placeholder="Enter Your Email" required /> <br />
<label>Password <span class="error">*</span></label>
<input type="password" name="password" class="text" placeholder="Enter Your password" required /> <br />
<input type="submit" name="user_registeration" value="SignUp" />
</form>
<?php if(isset($signUpError)){echo '<div>'.$signUpError.'</div>';}?>
<?php get_footer(); ?>
No comments:
Post a Comment