Skip to content

Instantly share code, notes, and snippets.

@nikitasinelnikov
Last active June 28, 2023 21:55
Show Gist options
  • Save nikitasinelnikov/cda758393164c76b82acbdb8536c1e84 to your computer and use it in GitHub Desktop.
Save nikitasinelnikov/cda758393164c76b82acbdb8536c1e84 to your computer and use it in GitHub Desktop.
JobBoardWP: Remove application field required attribute
// remove required mark from form
function remove_required_attr( $args ) {
foreach ( $args['sections']['job-details']['fields'] as &$field_data ) {
if ( 'job_application' !== $field_data['id'] ) {
continue;
}
$field_data['required'] = false;
}
return $args;
}
add_filter( 'jb_job_submission_form_args', 'remove_required_attr' );
// remove frontend form validation
function remove_application_validation( $posting_form ) {
if ( empty( $_POST['job_application'] ) && $posting_form->has_error( 'job_application' ) ) {
unset( $posting_form->errors[ 'job_application' ] );
}
}
add_action( 'jb-job-submission-validation', 'remove_application_validation' );
// remove backend required mark
function remove_admin_required_attr( $job_details_fields ) {
foreach ( $job_details_fields as &$field_data ) {
if ( 'jb-application-contact' !== $field_data['id'] ) {
continue;
}
unset( $field_data['description'] ); // if you want to remove description.
$field_data['required'] = false;
}
return $job_details_fields;
}
add_filter( 'jb_job_details_metabox_fields', 'remove_admin_required_attr' );
// remove backend metabox validation
function remove_admin_required_validation( $errors ) {
if ( empty( $_POST['data']['jb-application-contact'] ) ) {
if ( isset( $errors['empty'] ) ) {
$key = array_search( 'jb-application-contact', $errors['empty'], true );
if ( false !== $key ) {
unset( $errors['empty'][ $key ] );
if ( 0 === count( $errors['empty'] ) ) {
unset( $errors['empty'] );
}
}
}
} else {
if ( isset( $errors['wrong'] ) ) {
$key = array_search( 'jb-application-contact', $errors['wrong'], true );
if ( false !== $key ) {
unset( $errors['wrong'][ $key ] );
if ( 0 === count( $errors['wrong'] ) ) {
unset( $errors['wrong'] );
}
}
}
}
return $errors;
}
add_filter( 'jb_ajax_job_validation_errors', 'remove_admin_required_validation' );
@GrindamN
Copy link

Line 32 should have ;

@nikitasinelnikov
Copy link
Author

Just removed line 31, thanks

@nikitasinelnikov
Copy link
Author

nikitasinelnikov commented Jun 13, 2023

@GrindamN

To fully remove validation for the application field you may leave only this callback:

function remove_admin_required_validation( $errors ) {
	$key = array_search( $errors['empty'], 'jb-application-contact' );
	if ( false !== $key ) {
		unset( $errors['empty'][ $key ] );
	}
	return $errors;
}
add_filter( 'jb_ajax_job_validation_errors', 'remove_admin_required_validation' );

@GrindamN
Copy link

GrindamN commented Jun 13, 2023

Yeah I got that part from gist @nikitasinelnikov

However it still seems that the metabox is expecting some value to be inserted (either e-mail or https://url.com) so that the post can be updated. With the code above it acts the same like when validation is on, only without ajax check and notice error.

@nikitasinelnikov
Copy link
Author

@GrindamN I added some changes to the remove_admin_required_validation function and it should work as expected without any validation

@GrindamN
Copy link

@nikitasinelnikov Tried it. Now it doesn't work even you have empty field or if you put url or Email. Same as above.

@nikitasinelnikov
Copy link
Author

It removes all validations from the Application contact field. But if you need some of them you could remove some parts of this code or customize it in your necessary way.

If you need to remove only the "required" validation leave only this part:

        if ( empty( $_POST['data']['jb-application-contact'] ) ) {
		if ( isset( $errors['empty'] ) ) {
			$key = array_search( 'jb-application-contact', $errors['empty'], true );
			if ( false !== $key ) {
				unset( $errors['empty'][ $key ] );
				if ( 0 === count( $errors['empty'] ) ) {
					unset( $errors['empty'] );
				}
			}
		}
	}

@GrindamN
Copy link

Hmm. If possible I would remove the whole field, since I use FluentForms to collect the data in a separate custom post type.

@nikitasinelnikov
Copy link
Author

It's not a problem to delete this field from a forms

E.g. change the loop in the first function for the frontend:

foreach ( $args['sections']['job-details']['fields'] as $key => $field_data ) {
	if ( 'job_application' === $field_data['id'] ) {
		unset( $args['sections']['job-details']['fields'][ $key ] );
	}	
}

And similar changes for the admin field loop. Callbacks for validation leave the same but can unset errors without conditions in them.

@GrindamN
Copy link

Quick question: Would it be possible to prepopulate the field with https://test.com somehow?

@nikitasinelnikov
Copy link
Author

Hi @GrindamN

Sorry for the delay, you may repopulate the field via using the 'value' argument:

foreach ( $args['sections']['job-details']['fields'] as $key => &$field_data ) {
	if ( 'job_application' === $field_data['id'] && empty( $field_data['value'] ) ) {
		$field_data['value'] = 'https://test.com/';
	}	
}

Best Regards!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment