How to disable user email activation, and set User Password, First/Last Name, etc, from WP Job Manager Submit Form (!!!UNTESTED!!!)

functions.php

<?php

/**
 *	PLEASE NOTE THIS CODE IS UNTESTED AND IS ONLY MEANT TO BE USED AS A STARTING POINT !!!
 *
 *	CODE STILL NEEDS TO BE ADDED TO REMOVE OR PREVENT THE PASSWORD AND USER FIELDS FROM BEING SAVED TO THE LISTING!!
 *	I MAY JUST END UP TURNING THIS INTO AN OPEN SOURCE PLUGIN LATER ON, BUT AS OF NOW I DON'T HAVE TIME TO
 */


/**
 * This filter allows you to customize the user data before WP Job Manager adds the new user
 */
add_filter( 'job_manager_create_account_data', 'smyles_allow_set_password_from_submit_form' );

/**
 * Override core WP Job Manager wp_job_manager_notify_new_user() function
 */
add_action( 'after_setup_theme', array( $this, 'smyles_override_wp_job_manager_notify_new_user' ), 10 );

/**
 * Override core WP Job Manager notify function
 *
 * We have to prevent WP Job Manager from calling wp_new_user_notification() if using 4.3.1 or newer,
 * as it sets an activation key for the user in the DB, so we can bypass user account activation requirement.
 *
 * This function is called right before WP Job Manager defines its wp_job_manager_notify_new_user() function,
 * so we are essentially overriding it, and instead of using activation, we send a basic email with username
 * and password.
 *
 *
 */
function smyles_override_wp_job_manager_notify_new_user(){

	if ( ! function_exists( 'wp_job_manager_notify_new_user' ) ) :

		function wp_job_manager_notify_new_user( $user_id, $password ) {

			global $wp_version;

			if ( version_compare( $wp_version, '4.3.1', '<' ) ) {
				// This means you're using old version of WordPress that does not require activation yet
				wp_new_user_notification( $user_id, $password );

			} else {
				// Instead of calling wp_new_user_notification() we just send our own email
				$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
				$user     = get_userdata( $user_id );

				$message  = sprintf( __( 'Username: %s' ), $user->user_login ) . "rn";
				$message .= sprintf( __( 'Password: %s' ), $password ) . "rn";
				$message .= wp_login_url() . "rn";

				wp_mail( $user->user_email, sprintf( __( '[%s] Your username and password' ), $blogname ), $message );

			}
		}

	endif;

}

/**
 * Set User Values (including password) from Submit Form
 *
 * Available values that can be set are:
 * first_name, last_name, display_name, nickname, user_email, user_url, user_nicename, user_pass, description
 *
 * The key used inside the $_POST MUST be the exact meta key of the field you want to use for that value
 *
 * @param $new_user
 *
 * @return array
 */
function smyles_allow_set_password_from_submit_form( $new_user ) {
	
	// Password
	if( isset( $_POST['account_password'] ) ){
		$new_user['user_pass'] = $_POST['account_password'];
	}
	// First Name
	if( isset( $_POST['first_name'] ) ){
		$new_user['first_name'] = $_POST['first_name'];
	}
	// Last Name
	if( isset( $_POST['last_name'] ) ){
		$new_user['last_name'] = $_POST['last_name'];
	}

	return $new_user;

}

// Change filter below to submit_resume_form_validate_fields if you're using this for Resume submit instead of Job
add_filter( 'submit_job_form_validate_fields', 'smyles_check_user_passwords', 10, 3 );

/**
 * Verify that both password fields match
 *
 * This function verifies that both password fields match, and returns an error if they
 * do not.  This assumes you created two password fields on the form, with the meta key of
 * account_password and account_password2
 *
 *
 *
 * @param $true
 * @param $fields
 * @param $values
 *
 * @return WP_Error|boolean
 */
function smyles_check_user_passwords( $true, $fields, $values ){

	if( isset( $_POST['account_password'], $_POST['account_password2'] ) ){

		if( $_POST['account_password'] !== $_POST['account_password2'] ){
			return new WP_Error( 'validation-error', __( 'Passwords do not match, please try again.' ) );
		}

	}

	return $true;
}
No comments yet.

Leave a Reply