Support » Plugin: Advanced Custom Fields: Extended » Limit ability to publish form

  • Resolved rcwgsy

    (@rcwgsy)


    Hi,

    I see you have handled a query like this, but I am trying to get this to work with a custom role. Or any role actually.

    How would you edit this if you wanted a ‘customer’ to not be able to publish more than one CPT from a specific ACF form?

    I’m not getting any joy at the moment.

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

Viewing 15 replies - 1 through 15 (of 20 total)
  • Plugin Author hwk-fr

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    The topic you linked already does what you’re asking for. The code provided there only allow the “Author” role with less than 5 posts to see a form.

    You can adapt that code to use your custom role, with more or less posts allowed.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter rcwgsy

    (@rcwgsy)

    Does it have to be on the specific page where the form resides?

    Plugin Author hwk-fr

    (@hwk-fr)

    Hello,

    Yes, because this is where you will render the form using acfe_form('my-form') function. See Form integration documentation.

    Template page my-page.php usage example:

    <?php 
    // WordPress header
    get_header();
    ?>
    
    <?php
    
    // Display form to logged in user only (or any other condition)
    if(is_user_logged_in()){
        acfe_form('my-form');
    }
    ?>
    
    <?php 
    // WordPress footer
    get_footer();
    ?>

    Hope it helps!

    Regards.

    Thread Starter rcwgsy

    (@rcwgsy)

    Can we not just add this to snippets and then call on a php function maybe?

    I’ve capped the areas I think I should be editing to ensure a customer can only post once to a specific form, can you advise if I am even remotely close?!

    $user = wp_get_current_user();
    
    // Check if current user has role 'CUSTOMER'
    if(in_array('CUSTOMER', (array) $user->roles)){
        
        // Get posts of the current user (by post_author)
        $check_posts = get_posts(array(
            'post_type'         => 'POSTTYPENAME',
            'post_author'       => get_current_user_id(),
            'posts_per_page'    => -1,
            'fields'            => 'ids'
        ));
    
        // Display form if the current has less than 1 posts
        if(count($check_posts) < 2){
            
            // Call my form
            acfe_form('MY-FORM-SLUG');
            
        }else{
            
            ?>
            <h3>You already published a post.</h3>
            <?php
            
        }
    
    }
    Plugin Author hwk-fr

    (@hwk-fr)

    Hello,

    Yes this code looks correct and should be included on the template page that will display the form (See the usage example in my answer above).

    You cannot inlude that logic in a function tho. If you display the form using a shortcode in a WYSIWYG editor, you’ll have to add that logic in the acfe/form/load hook. See documentation.

    In your case it would be:

    add_filter('acfe/form/load/form=MY-FORM-SLUG', 'my_form_settings', 10, 2);
    function my_form_settings($form, $post_id){
    
        // get current user
        $user = wp_get_current_user();
        
        // check for current user role CUSTOMER
        if(in_array('CUSTOMER', (array) $user->roles)){
            
            // get posts of the current user (by post_author)
            $check_posts = get_posts(array(
                'post_type'         => 'POSTTYPENAME',
                'post_author'       => get_current_user_id(),
                'posts_per_page'    => -1,
                'fields'            => 'ids'
            ));
    
            // count posts and display form if the user has less than 2 posts
            if(count($check_posts) < 2){
                
                // display the form
                return $form;
                
            }
    
        }
    
        // hide the form in any other case
        return false;
        
    }
    

    Regards.

    Thread Starter rcwgsy

    (@rcwgsy)

    Thanks, getting there!

    What I am hoping to do is add the code to snippets.

    And then use a PHP function condition in Oxygen Builder to hide the form or section depending on whether the logged in author has posted or not.

    Plugin Author hwk-fr

    (@hwk-fr)

    Hello,

    As explained above, this is not a code snippet that can be used as a function.

    If you’re working with a page builder, I would recommend to simply use the [acfe_form name="my-form"] shortcode in your builder (See shortcode integration documentation), and add the hook from my answer above to add a conditional logic that will decide if the form should be rendered or not.

    Regards.

    Thread Starter rcwgsy

    (@rcwgsy)

    Sorry, I’m lost again.

    Have got the shortcode added in a div within a page.

    Where and I putting this:

    add_filter('acfe/form/load/form=MY-FORM-SLUG', 'my_form_settings', 10, 2);
    function my_form_settings($form, $post_id){
        
        // check for current user role CUSTOMER
        if(in_array('CUSTOMER', (array) $user->roles)){
            
            // get posts of the current user (by post_author)
            $check_posts = get_posts(array(
                'post_type'         => 'POSTTYPENAME',
                'post_author'       => get_current_user_id(),
                'posts_per_page'    => -1,
                'fields'            => 'ids'
            ));
    
            // count posts and display form if the user has less than 2 posts
            if(count($check_posts) < 2){
                
                // display the form
                return $form;
                
            }
    
        }
    
        // hide the form in any other case
        return false;
        
    }
    Plugin Author hwk-fr

    (@hwk-fr)

    Hello,

    You should put WordPress hooks in your theme functions.php file.

    Since this is an important concept in WordPress development, and as it’s outisde of this forum support field of competence, I would recommend to read more about WordPress hooks documentation here.

    You’ll also find tutorials online:

    WordPress Actions, Filters, and Hooks : A guide for non-developers
    WordPress Hooks, Actions, and Filters: What They Do and How They Work
    What is: Hooks

    Hope it helps!

    Regards.

    Thread Starter rcwgsy

    (@rcwgsy)

    I appreciate your time on this.

    I have done that and it hides the form for everyone not just the user role.

    Plugin Author hwk-fr

    (@hwk-fr)

    Hello,

    Yes, because the the code “hide the form in any other case” (outside of the CUSTOMER user role) as described in the code comments. You’ll have to adapt your if() statement to display the form depending on your needs.

    It looks like what you’re asking for is:

    add_filter('acfe/form/load/form=MY-FORM-SLUG', 'my_form_settings', 10, 2);
    function my_form_settings($form, $post_id){
    
        // get current user
        $user = wp_get_current_user();
        
        // check for current user role CUSTOMER
        if(in_array('CUSTOMER', (array) $user->roles)){
            
            // get posts of the current user (by post_author)
            $check_posts = get_posts(array(
                'post_type'         => 'POSTTYPENAME',
                'post_author'       => get_current_user_id(),
                'posts_per_page'    => -1,
                'fields'            => 'ids'
            ));
    
            // CUSTOMER has at least 1 post, hide the form
            if(count($check_posts) > 0){
                
                // hide the form
                return false;
                
            }
    
        }
    
        // display form
        return $form;
        
    }
    

    Note that I cannot guarantee that’s exactly what you’re looking for as I don’t have the specifications document of your project and I can’t do custom development for every users.

    You’ll have to do some tests, trial & error, and adapt the code on your own here.

    I hope you’ll understand.

    Regards.

    Thread Starter rcwgsy

    (@rcwgsy)

    No, I understand, thanks for your time.

    That hasn’t worked for me on this occasion, think I review other options.

    Plugin Author hwk-fr

    (@hwk-fr)

    I’m sorry to hear that.

    You may try to get some help from a developer to help you code the conditional logic you need. You could also ask some help on different development communities such as the WordPress development forum or WP Stackoverflow.

    Regards.

    Thread Starter rcwgsy

    (@rcwgsy)

    Will do, thank you.

    Thread Starter rcwgsy

    (@rcwgsy)

    What about changing the users role, after then publish a form?

    Can you do that?

    I can then setup conditions to the div to hide/show forms based on user role.

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Limit ability to publish form’ is closed to new replies.