Issue with User Roles and Vendor Creation

I need to charge a fee when users join the directory. Since HivePress didn’t offer this option, I found a workaround using Forminator forms.

I added custom code to automatically create a new vendor in HivePress when a user with registers.

This part is working well. However, when users (Not designer) sign up to message or leave a review via the HivePress registration form, they are also being assigned the “Contributor” role and given a vendor profile, which I do not want.

I tried changing the default role for new users to “Subscriber,” but this impacted the Forminator form as well, preventing designers from signing up correctly. I also tried fixing with code but it didn’t work.

Current Setup:

  1. Forminator Forms: Used to handle the registration process and charge a fee for designers
  2. HivePress: Used to manage vendors.
  3. Custom Code: Automatically creates a vendor in HivePress when a user with the “Contributor” role is registered.

Goal:

  1. Ensure that users registering via HivePress are not assigned the “Contributor” role and assigned ‘Subscriber instead’ so a vendor is not created.
  2. Maintain the ability for users registering via Forminator to be assigned the “Contributor” role and be created as vendors.

Here is the original code that I added:

// Create a new vendor when a user registers
add_action('user_register', 'create_hivepress_vendor', 10, 1);

function create_hivepress_vendor($user_id) {
    if (!function_exists('hivepress')) {
        return;
    }

    // Create a new vendor post
    $vendor_id = wp_insert_post([
        'post_title' => get_user_meta($user_id, 'nickname', true),
        'post_type' => 'hp_vendor',
        'post_status' => 'publish',
        'post_author' => $user_id
    ]);

    if (is_wp_error($vendor_id)) {
        error_log('Error creating vendor: ' . $vendor_id->get_error_message());
        return;
    }

    // Update vendor meta
    update_post_meta($vendor_id, 'hp_user', $user_id);
    update_user_meta($user_id, 'hp_vendor', $vendor_id);

    // Assign vendor role
    $user = new WP_User($user_id);
    $user->add_role('vendor');
}

Any help with this will be greatly appreciated.

Hi,

Sorry, there’s no simple code snippet for this, it would require a custom implementation. If customizations beyond the available features are required for your site, please consider hiring someone for custom work: Experts | HivePress

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