Make image required for vendors only

So i want to make it mandatory for freelancers/vendors to upload a profile picture, but i dont want employers/buyers to be mandatory to upload. The reason for this is i think its good if freelancers are forced to upload a picture to help with their profile, but buyers shouldnt have to. I used this code in the functions.php but it makes it mandatory for all users. Anyway i can just make it specific to vendors?


add_filter(
	'hivepress/v1/models/host',
	function( $model ) {
		if ( isset( $model['fields']['image'] ) ) {
			$model['fields']['image']['required'] = true;
		}

		return $model;
	},
	1000
);

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

		if ( $user && ! $user->get_image__id() ) {
			$errors[] = 'Por favor agrega una foto de perfil.';
		}

		return $errors;
	},
	100,
	2
);

Please try this PHP snippet

add_filter(
	'hivepress/v1/forms/user_update/errors',
	function( $errors, $form ) {
		if(!empty($errors)){
			return $errors;
		}
		
		$vendor = \HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get();
		
		if(!$vendor){
			return $errors;
		}
		
		$user = $form->get_model();

		if ( $user && ! $user->get_image__id() ) {
			$errors[] = 'Please upload the profile image.';
		}

		return $errors;
	},
	100,
	2
);

add_filter(
	'hivepress/v1/forms/user_update',
	function( $form ) {
		$vendor = \HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get();
		
		if(!$vendor){
			return $form;
		}
		
		$form['fields']['image']['statuses']['optional'] = null;

		return $form;
	},
	1000
);
1 Like

This diddn’t work for me. Regular users still get the error when trying to save the profile information

Please try this PHP snippet instead

add_filter(
	'hivepress/v1/forms/user_update/errors',
	function( $errors, $form ) {
		if(!empty($errors)){
			return $errors;
		}
		
		$vendor = \HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get_first_id();
		
		if(!$vendor){
			return $errors;
		}
		
		$user = $form->get_model();

		if ( $user && ! $user->get_image__id() ) {
			$errors[] = 'Please upload the profile image.';
		}

		return $errors;
	},
	100,
	2
);

add_filter(
	'hivepress/v1/forms/user_update',
	function( $form ) {
		$vendor = \HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get_first_id();
		
		if(!$vendor){
			return $form;
		}
		
		$form['fields']['image']['statuses']['optional'] = null;

		return $form;
	},
	1000
);
1 Like

@yevhen - thank you, this works better.

There is only one issue with the implementation: If a vendor has a profil picture set, and then goes to account/settings and removes that picture, as expected he/she will get the error message - However, upon removing the picture, the profil form is saved, even though the click of a button generates an error, so if the vendor does not follow the suggestion of the error message, and just leaves the settings page, he/she successsfully deleted the profile picture.

edit I would also like to add that when going from a user to a vendor, and completing your profile, the image is not required either, so this snippet doesn’t really work

  1. Please try this PHP snippet instead to fix the problem with the complete profile form. But please note that it can require further customization and additional testing on your website
add_filter(
	'hivepress/v1/forms/user_update/errors',
	function( $errors, $form ) {
		if(!empty($errors)){
			return $errors;
		}
		
		$vendor = \HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get_first_id();
		
		if(!$vendor){
			return $errors;
		}
		
		$user = $form->get_model();

		if ( $user && ! $user->get_image__id() ) {
			$errors[] = 'Please upload the profile image.';
		}

		return $errors;
	},
	100,
	2
);

add_filter(
	'hivepress/v1/forms/user_update',
	function( $form ) {
		$vendor = \HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get_first_id();
		
		if(!$vendor){
			return $form;
		}
		
		$form['fields']['image']['statuses']['optional'] = null;

		return $form;
	},
	1000
);

add_filter(
	'hivepress/v1/models/vendor/fields',
	function($fields){
		if(isset($fields['image'])){
			$fields['image']['required'] = true;
		}
		
		return $fields;
	},
	1000
);
  1. Please try this CSS snippet to hide the button to delete the profile image in the account settings
.hp-form--user-update .hp-field--attachment-upload a{
	display: none !important;
}

Thanks for the hard work and suggestios @yevhen .

With your suggested code and some minor tweaks, we’re almost at the point where it works now! The only issue now is that when going from a regular user to a vendor profil, in the complete profil form, the UX is bad because image is still tagged as “optional” and when trying to submit the profil, there is no error message showing.

Anyways, I’ve made a small change to the CSS suggestion, removing the delete profile image button only for Vendors. This means that regular users that uploads an optional profil picture still sees this delete button and don’t have to commit to having a profile picture ones they upload one. For other people wanting to implement the same feature, you need to add this php snippet to add the current vendor role to the body element. Then you can selectivly hide the delete button only for contributors(vendors).

// Creates CSS classes for the User Roles. user-role-*insert user role here*
add_filter('body_class', 'body_class_user_role');

function body_class_user_role($classes)
{

    if (is_user_logged_in()) {
        $user = wp_get_current_user();
        $roles = $user->roles;
        $classes[] = 'user-role-' . $roles[0];
    }
    return $classes;
}

and then the CSS will look as following:

body.logged-in.user-role-contributor .hp-form--user-update .hp-field--attachment-upload a {
  display: none !important;
}

EDIT
There is some inbetween problematics with this solution. First of, when a listing have been submitted, but not yet aproved, the “to-be” vendor (still a regular user) can delete the profile picture.

Also, there’s another issue with a CSS solution where one can run an extension to show hidden elements, or in my case, remove the user-role-contributor class from the body element, and then the delete button will reappear. A more bullet proof solution would be prefered, if anyone has any suggestions

1 Like

Thanks for sharing. To hide the optional status, please try overriding the field statuses parameter:

'statuses' => ['optional'=>null],

In what Hook should this be used? It is used within an if statement in the user_update Hook, but doesn’t work when users shall become vendors through the complete profile form. Also, I would like to display an error message in this form aswell.

Can you help out?

Please make sure that you correctly put this PHP snippet with the Code snippets plugin or the similar one

add_filter(
	'hivepress/v1/forms/user_update/errors',
	function( $errors, $form ) {
		if(!empty($errors)){
			return $errors;
		}
		
		$vendor = \HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get_first_id();
		
		if(!$vendor){
			return $errors;
		}
		
		$user = $form->get_model();

		if ( $user && ! $user->get_image__id() ) {
			$errors[] = 'Please upload the profile image.';
		}

		return $errors;
	},
	100,
	2
);

add_filter(
	'hivepress/v1/forms/user_update',
	function( $form ) {
		$vendor = \HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get_first_id();
		
		if(!$vendor){
			return $form;
		}
		
		$form['fields']['image']['statuses']['optional'] = null;

		return $form;
	},
	1000
);

add_filter(
	'hivepress/v1/models/vendor/fields',
	function($fields){
		if(isset($fields['image'])){
			$fields['image']['required'] = true;
		}
		
		return $fields;
	},
	1000
);

It was tested locally and it seems to be ok

The Complete Profile page

The account settings page

I have done some more testing, and the code provided only works when the current user is a contributor/vendor from before, and he’s profile misses an essencial attribute to “Complete Profile”. Say a Vendor removes a required attribute, and then clicks on “submit a listing”. Then he will be redirected to the “Complete Profile” form, and THEN this code snippet works.

On a freshly created profile, and when the “user”/“subscriber” is trying to upload a listing, he is redirected to the same “Complete Profile” page, BUT then this code snippet does not work.

The Complete Profile page that does work has a class of data-select2-id="9" in its body tag

The Complete Profile page that does NOT work has a class of customize-support in its body tag

thats the difference I can spot. What this means - I have no clue.

Please try this PHP snippet instead

add_filter(
	'hivepress/v1/forms/user_update/errors',
	function( $errors, $form ) {
		if(!empty($errors)){
			return $errors;
		}
		
		$vendor = \HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get_first_id();
		
		if(!$vendor){
			return $errors;
		}
		
		$user = $form->get_model();

		if ( $user && ! $user->get_image__id() ) {
			$errors[] = 'Please upload the profile image.';
		}

		return $errors;
	},
	100,
	2
);

add_filter(
	'hivepress/v1/forms/user_update_profile/errors',
	function( $errors, $form ) {
		if(!empty($errors)){
			return $errors;
		}
		
		$user = $form->get_model();

		if ( $user && ! $user->get_image__id() ) {
			$errors[] = 'Please upload the profile image.';
		}

		return $errors;
	},
	100,
	2
);

add_filter(
	'hivepress/v1/forms/user_update',
	function( $form ) {
		$vendor = \HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get_first_id();
		
		if(!$vendor && 'listing_submit_profile_page' !== hivepress()->router->get_current_route_name()){
			return $form;
		}
		
		$form['fields']['image']['statuses']['optional'] = null;

		return $form;
	},
	1000
);

add_filter(
	'hivepress/v1/models/vendor/fields',
	function($fields){
		if(isset($fields['image'])){
			$fields['image']['required'] = true;
		}
		
		return $fields;
	},
	1000
);

This now works correctly for the initial load of the form, when the user is a subscriber/regular user and is Completing Profile to become a vendor. However, when choosing a Vendor Category, the form reloads with that Vendor Category spesific attributes (as intended), but then we are back to the image being “optional” and the body tag having the "data-select2-id=“12” " attribute.

Please try this PHP snippet instead

add_filter(
	'hivepress/v1/forms/user_update/errors',
	function( $errors, $form ) {
		if(!empty($errors)){
			return $errors;
		}
		
		$vendor = \HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get_first_id();
		
		if(!$vendor){
			return $errors;
		}
		
		$user = $form->get_model();

		if ( $user && ! $user->get_image__id() ) {
			$errors[] = 'Please upload the profile image.';
		}

		return $errors;
	},
	100,
	2
);

add_filter(
	'hivepress/v1/forms/user_update_profile/errors',
	function( $errors, $form ) {
		if(!empty($errors)){
			return $errors;
		}
		
		$user = $form->get_model();

		if ( $user && ! $user->get_image__id() ) {
			$errors[] = 'Please upload the profile image.';
		}

		return $errors;
	},
	100,
	2
);

add_filter(
	'hivepress/v1/forms/user_update',
	function( $form ) {
		$vendor = \HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get_first_id();
		
		if(!$vendor && 'listing_submit_profile_page' !== hivepress()->router->get_current_route_name()){
			return $form;
		}
		
		$form['fields']['image']['statuses']['optional'] = null;

		return $form;
	},
	1000
);

add_filter(
	'hivepress/v1/forms/user_update_profile',
	function( $form ) {
		$form['fields']['image']['statuses']['optional'] = null;

		return $form;
	},
	1000
);

add_filter(
	'hivepress/v1/models/vendor/fields',
	function($fields){
		if(isset($fields['image'])){
			$fields['image']['required'] = true;
		}
		
		return $fields;
	},
	1000
);

Thanks alot yevhen - This works great!

I’ll do some more testing, but this seemes solid :slight_smile: