Make the First Name, Last Name, Photo, Description fields mandatory when making a request by a user (not a seller)

Ihor hello. Tell me how to make the First Name, Last Name, Photo, Description fields mandatory when making a request by a user (not a seller). That when clicking on the request, it would be required to fill in all these attributes. This snippet works only with listings and sellers for some reason:

add_filter(
	'hivepress/v1/forms/user_update',
	function( $form ) {
		$form['fields']['image']['required'] = true;
		$form['fields']['first_name']['required'] = true;
		$form['fields']['last_name']['required'] = true;
		$form['fields']['description']['required'] = true;
		return $form;
	},
	1000
);

add_filter(
	'hivepress/v1/forms/user_update/errors',
	function( $errors, $form ) {
		$user = $form->get_model();

		if ( $user && ! $user->get_image__id() && current_user_can('edit_posts') ) {
			$errors[] = 'Загрузите пожалуйста фотографию.';
		}

		return $errors;
	},
	100,
	2
);

I’ll add more here to make it clearer what I need to implement:

  1. When the user is registered he is redirected to create a request. I want him to not be able to create a request until he uploads his photo.
  2. If the user has no photo and clicks to create a request, he must be sent to the profile with a proposal to upload a photo.

The idea is that this function will work if I create any attribute and specify that it is required. But I need it to work with a photo.

And another question, is it possible to create a page where all users will be displayed. Not sellers.
Thank you.

Another important question for me . I manage to limit requests for all users and allow a certain number for verified users. But I need to implement another model within the engine and need help.

How to limit queries only for seller status(role) of sellers and allow for users in the regular role? Very much need specific help, thanks in advance.

  1. If you mean restriction only for regular users (not vendors) then please try this PHP snippet
add_filter(
	'hivepress/v1/forms/request_submit/errors',
	function( $errors, $form ) {
		
		if(!current_user_can('edit_posts')){
			$request = $form->get_model();
			
			if(!$request){
				return $errors;
			}
			
			$user = $request->get_user();
			$error_fields = [];
			
			if(!$user->get_first_name()){
				$error_fields[] = 'First Name';	
			}
			if(!$user->get_last_name()){
				$error_fields[] = 'Last Name';	
			}
			if(!$user->get_description()){
				$error_fields[] = 'Description';	
			}
			if(!$user->get_image_id()){
				$error_fields[] = 'Profile Image';	
			}
			
			if($error_fields){
				$errors[] = 'Please fill in these fields in profile settings before posting a request '.implode(', ', $error_fields);
			}
		}
		return $errors;
	},
	1000,
	2
);
  1. Yes, it is possible but this requires advanced customizations. If you are familiar with code customization please try customizing it using the collection of code snippets Search · user:hivepress · GitHub and other developer resources, or consider hiring someone for custom work https://fwd.cx/hLhc73mQCD9R

  2. Please clarify if you want to restrict the number of requests only to vendors?

2 Likes

Hi, Yevhen

  1. Yes, that’s it. Thank you very much.
  2. Thank you, I will try it. But I was hoping for a more specific tip:)
  3. I managed to implement this function with this code, while waiting for an answer. But I am interested in this feature with a link to the user’s role.
add_filter( 
	'hivepress/v1/templates/site_header_block/blocks',
	function( $blocks, $template ) {
		if(is_user_logged_in() && !current_user_can('edit_posts')){
			$blocks = hivepress()->helper->merge_trees(
					[ 'blocks' => $blocks ],
					[
						'blocks' => [
							'listing_submit_link' => [
								'type' => 'content',
							],
						],
				]
				)['blocks'];
		}
		
		if(hivepress()->get_version( 'requests' ) && is_user_logged_in() && current_user_can('edit_posts')){
			$blocks = hivepress()->helper->merge_trees(
					[ 'blocks' => $blocks ],
					[
						'blocks' => [
							'request_submit_link' => [
								'type' => 'content',
							],
						],
				]
				)['blocks'];
		}
		
		return $blocks;
	},
    1000,
	2
);
  1. Yevgen. Is it possible that when you select a request category, specific attributes for that category would appear? If yes, I would be grateful for tips on how to implement it. Thank you.
  1. As regular users do not have public accounts so advanced customization is needed. To start, it is possible to try this WordPress function get_users() | Function | WordPress Developer Resources to display users. If you are not familiar with code customization then please consider hiring someone for custom work https://fwd.cx/hLhc73mQCD9R

  2. To make changes only for regular users it is possible to add this condition

if ( !current_user_can('edit_posts') ) {
    // your code here.
}
  1. Please make sure that you have the latest HivePress Requests extension version. It should work in this way by default

Yevhen, hi. I still have a problem with this field. When a simple user fills out a request and clicks send, he gets a message to fill in the required fields in the user profile: First Name, Last Name and Photo.

So the photo error does not disappear even when the user inserts a photo. Still, when filling out the request, he gets an error where he is asked to fill in a photo.

  • I think I found the problem, this is how it works:
if(!$user->get_image__id()){
				$error_fields[] = 'Profile Image';	
			}

I had to add this to your snippet to make sure the profile didn’t go through without the Photo:

add_filter(
	'hivepress/v1/forms/user_update/errors',
	function( $errors, $form ) {
		$user = $form->get_model();

		if ( $user && ! $user->get_image__id() ) {
			$errors[] = 'Profile Image';
		}

		return $errors;
	},
	100,
	2
);

add_filter(
	'hivepress/v1/forms/user_update',
	function( $form ) {
		$form['fields']['image']['statuses']['optional'] = null;
		$form['fields']['image']['required'] = true;
		$form['fields']['first_name']['required'] = true;
		$form['fields']['last_name']['required'] = true;
		$form['fields']['description']['required'] = true;
		return $form;
	},
	1000
);

It works that way, but does it look correct. Maybe you can write it shorter and more intelligently?

If you mean restriction only for regular users (not vendors) as mentioned above then please try this PHP snippet

add_filter(
	'hivepress/v1/forms/request_submit/errors',
	function( $errors, $form ) {
		
		if(!current_user_can('edit_posts')){
			$request = $form->get_model();
			
			if(!$request){
				return $errors;
			}
			
			$user = $request->get_user();
			
			if(!$user){
				return $errors;
			}
			
			$error_fields = [];
			
			if(!$user->get_first_name()){
				$error_fields[] = 'First Name';	
			}
			if(!$user->get_last_name()){
				$error_fields[] = 'Last Name';	
			}
			if(!$user->get_description()){
				$error_fields[] = 'Description';	
			}
			if(!$user->get_image__id()){
				$error_fields[] = 'Profile Image';	
			}
			
			if($error_fields){
				$errors[] = 'Please fill in these fields in profile settings before posting a request '.implode(', ', $error_fields);
			}
		}
		return $errors;
	},
	1000,
	2
);

add_filter(
	'hivepress/v1/forms/user_update',
	function( $form ) {
		$form['fields']['image']['statuses']['optional'] = null;
		$form['fields']['image']['required'] = true;
		$form['fields']['first_name']['required'] = true;
		$form['fields']['last_name']['required'] = true;
		$form['fields']['description']['required'] = true;
		return $form;
	},
	1000
);

Thx,Yevhen :+1:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.