Skip Navigation

[Closed] Using front-end forms to approve or reject user applications

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 3 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#1901965

I have a post form which is a registration form .. users should register to apply for our service ..
We also have admin people who should review the applications and accept or reject them..

Admin user should access the page where all registrations there directly after they login..

the issue is that our website should be accessed using VPN . and we don't want the admins to use the VPN we want them to access it directly ..

#1903529

Hi Salim,

Thank you for waiting.

I was able to make this work on my test website, using the following steps:

1. In a custom post type "User Applications", I added these custom fields

- user-email (email type)
- user-first-name (single line type)
- user-last-name (single line type)
- user-created (single line type)
- application-status (radio type)
--- field options
-----Under Review (value '1', default)
-----Accpeted (value '2')
-----Rejected (value '3')

Note: The "user-created" will help in keeping track of whether a user has been created already for a particular "User Application" post or not.

2. Next, I created an edit form for "User Application" and only included the "application-status" field and the submit button.

3. After that, I used the "cred_save_data" hook ( https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data ) and the "wp_insert_user" function ( https://developer.wordpress.org/reference/functions/wp_insert_user/ ) to programmatically add a new user, when that form is submitted with Accpeted (value '2') option:


add_action( 'cred_save_data', 'custom_user_author_update', 10, 2 );
function custom_user_author_update( $post_id, $form_data ) {
	if ($form_data['id']==12345) {
		// get value of field to check if user has been created already through user-created field
		$user_created = get_post_meta($post_id, 'wpcf-user-created', true);

		// if field value is approved and user is not already created
		if( ($_POST['wpcf-application-status'] == '2') && ($user_created != 'yes') ) {

			$user_name = get_post_meta($post_id, 'wpcf-user-email', true);
			$user_email = get_post_meta($post_id, 'wpcf-user-email', true);
			$user_fname = get_post_meta($post_id, 'wpcf-user-first-name', true);
			$user_lname = get_post_meta($post_id, 'wpcf-user-last-name', true);

			$userdata = array(
				'user_login' =>  $user_email,
				'user_pass'  =>  NULL, // When creating an user, `user_pass` is expected.
				'user_email' =>  $user_email,
				'first_name' =>  $user_fname,
				'last_name' =>  $user_lname,
				'role' =>  'subscriber'
			);
			// insert new user
			$user_id = wp_insert_user( $userdata ) ;
			 
			// On success send new user notification to user and admin and update yes in the user-created custom field
			if ( ! is_wp_error( $user_id ) ) {
			    wp_new_user_notification( $user_id, null, 'both' );
			    update_post_meta( $post_id, 'wpcf-user-created', 'yes' );
			}
		}
	}
}

Please replace "12345" with your actual edit form's ID and adjust the code as per your custom fields.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1904847

Thank you for your support Waqar, I will work to customize the code as per our form. But if it is possible to share your test site to see how it works.

#1904885

You're very welcome.

I'm afraid, I tested this on a local test website and it has been overwritten since then.

You can try out these steps on your website and if some part doesn't work, you can share the details and admin access, and I'll see what is missing.

The topic ‘[Closed] Using front-end forms to approve or reject user applications’ is closed to new replies.