WordPress Register Form Without Plugin

WordPress Register Form Without Plugin
719 Views
0
(0)

Are you need simple register form without plugin for your WordPress website? Yes, Then i have created [cxc_register_form] shortcode with quick simple code. So you can add register form anywhere like in Pages, Posts and Widget etc… Therefore you just need to put [cxc_register_form] shortcode where you need register form. So you need to follow below steps.

Code for shortcode.

You need to put below code in theme’s functions.php file. If you have created child theme then add this in function file of child theme.

Register Form | Shortcode: [cxc_register_form]

<?php
  add_shortcode( 'cxc_register_form', 'cxc_register_form_callback' );

function cxc_register_form_callback() {
	ob_start();

	if ( !is_user_logged_in() ) {

		global $registrationError, $registrationSuccess;

		if ( !empty( $registrationError ) ) {
			?>
			<div class="alert alert-danger">
				<?php echo $registrationError; ?>
			</div>
		<?php } ?>

		<?php if ( !empty( $registrationSuccess ) ) { ?>
			<br/>
			<div class="alert alert-success">
				<?php echo $registrationSuccess; ?>
			</div>
		<?php } ?>

		<form method="post" class="wc-register-form">
			<div class="register_form">
				<div class="log_user">
					<label for="user_name">User name</label>
					<?php $user_name = isset($_POST['user_name']) ? $_POST['user_name'] : ''; ?>
					<input type="text" name="user_name" id="user_name" value="<?php echo $user_name; ?>" />
				</div>

				<div class="log_user">
					<label for="user_email">Email address</label>
					<?php $user_email = isset($_POST['user_email']) ? $_POST['user_email'] : ''; ?>
					<input type="email" name="user_email" id="user_email" value="<?php echo $user_email; ?>" />
				</div>

				<div class="log_pass">
					<label for="user_password">Password</label>
					<input type="password" name="user_password" id="user_password" />
				</div>

				<div class="log_pass">
					<label for="user_cpassword">Password again</label>
					<input type="password" name="user_cpassword" id="user_cpassword" />
				</div>

				<div class="log_pass">
					<?php
					ob_start();
					do_action( 'register_form' );
					echo ob_get_clean();
					?>
				</div>

				<div class="log_user">
					<?php wp_nonce_field( 'userRegister', 'formType' ); ?>
					<button type="submit" class="register_user">Register</button>
				</div>
			</div>
		</form>
		<?php
	} else {
		echo '<p class="error-logged">You are already logged in.</p>';
	}
	$register_form = ob_get_clean();
	return $register_form;
}
?>

Code for Register

In this code i am validating form and sign up in WordPress on wp action. Add it in functions.php file after above code. If this code will not work then change action wp to init.

<?php
add_action( 'wp', 'cxc_user_register_callback' );

function cxc_user_register_callback() {
	if ( isset( $_POST['formType'] ) && wp_verify_nonce( $_POST['formType'], 'userRegister') ) {

		global $registrationError, $registrationSuccess;

		$u_name = trim( $_POST['user_name'] );
		$u_email = trim( $_POST['user_email'] );
		$u_pwd = trim( $_POST['user_password'] );
		$u_cpwd = trim( $_POST['user_cpassword'] );

		if ( $u_name == '' ) {
			$registrationError .= '<strong>Error! </strong> Enter User name.,';
		}

		if ( username_exists( $u_name ) ) {
			$registrationError .= '<strong>Error! </strong> Username In Use!.,';
		}

		if ( $u_email == '' ) {
			$registrationError .= '<strong>Error! </strong> Enter Email.,';
		}

		if ( $u_pwd == '' || $u_cpwd == '' ) {
			$registrationError .= '<strong>Error! </strong> Enter Password.,';
		}

		if ( strlen( $u_pwd ) < 7) {
			$registrationError .= '<strong>Error! </strong> Use minimum 7 character in password.,';
		}

		if ( $u_pwd != $u_cpwd ) {
			$registrationError .= '<strong>Error! </strong> Password are not matching.,';
		}

		if ( $u_email != '' && !is_email( $u_email ) ) {
			$registrationError .= '<strong>Error! </strong> Invalid e-mail address.,';
		}

		if ( email_exists( $u_email ) != false ) {
			$registrationError .= '<strong>Error! </strong> This Email is already registered.,';
		}

		$registrationError = trim( $registrationError, ',' );
		$registrationError = str_replace( ",", "<br/>", $registrationError );

		if ( empty( $registrationError ) ) {

			$user_login = $u_name;
			$user_email = $u_email;

			$userdata = array(
				'user_login' => $user_login,
				'user_pass' => $u_pwd,
				'user_email' => $user_email
			);

			$user_id = wp_insert_user( $userdata );

			if ( is_wp_error( $user ) ) {
				$registrationError = $user->get_error_message();
			} else {
				$registrationSuccess = '<strong>Success! </strong> Application submitted. Please wait for user approval.';	
				$user = get_userdata( $user_id );						
				wp_set_current_user( $user->ID );
				wp_set_auth_cookie( $user->ID );
				do_action( 'wp_login', $user->user_login, $user );
				wp_redirect( site_url() );
				exit;
			}
		}
	}
}
?>

How useful was this blog?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this blog.

Leave a comment

Your email address will not be published. Required fields are marked *