• Resolved kioubizin

    (@kioubizin)


    Hello,

    I am using the pro version of ACF Extended, not sure if it’s best to submit a ticket through the pro account or here in the forum as I think other users here may help as well if they have the solution!

    So, I have 3 user roles on my site:

    • Start-Up
    • Growing
    • Enterprise

    I have 4 post types:

    • Companies
    • Services
    • Promo Codes
    • Jobs

    Each of these post types have their own frontend form submission through the user dashboard and the dashboard access is controlled by user role which is managed through another access plugin.

    Now, what I want to achieve is:

    • If it’s the user role Start-Up to only be able to submit 1 company + 1 service only
    • If it’s the user role Growing to only be able to submit 1 company, 5 services, 1 promo code and 1 job
    • If it’s the user role Enterprise, no limitations…

    If the user role exceeds the limitation, to redirect the user to a page that we’ll be creating so they can upgrade their account/user role…

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    Since you fully control where you display your form using the acfe_form() function or the [acfe_form] shortcode, you can totally use custom PHP condition to display a form, or not, based on your own logic. See Form PHP Conditional Render documentation.

    In your case, here is a simple code that check if the currently logged user has the role “startup”, and if he has no post published in the post type “company”.

    This code use the get_userdata() function (see documentation) to retrieve user roles, and then the count_user_posts() function (see documentation) to count user posts in specific post types.

    // set default var
    $show_form = false;
    
    // get current user id
    $user_id = get_current_user_id();
    
    // get user data
    $user_data = get_userdata($user_id);
    
    // get user roles
    $user_roles = (array) $user_data->roles;
    
    // check user has role 'startup'
    if(in_array('startup', $user_roles)){
        
        // count user posts in post type: 'company'
        $count = (int) count_user_posts($user_id, array('company'));
        
        // if no company
        // show form
        if($count < 1){
            $show_form = true;
        }
        
    }
    
    // display form conditionally
    if($show_form){
        
        acfe_form(array(
            'name' => 'create-company'
        ));
        
    }

    Since you have other conditions, you might would like to create your own function to do that logic outside of the template.

    Note that if you’re using the [acfe_form] shrotcode to display your form, you will have to use such code inside the acfe/form/load hook, to conditionally display your form via shortcode. See Form Shortcode Conditional Render documentation.

    Additionally, if you prefer to redirect the user if he doesn’t meet the requirements, you’ll have to use a wp_redirect() function (see documentation) inside the template_redirect hook (see documentation), because server redirections have to done before headers are sent (before the page started to be rendered, before the <head> tag). You could use the same logic in that hook to determine if you have to redirect the user or not.

    Hope it helps!

    Have a nice day!

    Regards.

Viewing 1 replies (of 1 total)
  • The topic ‘Limit Number of Submission Based on User Role’ is closed to new replies.