Support

Account

Home Forums ACF PRO Can I use acf_form() to create a comment instead of a post?

Solved

Can I use acf_form() to create a comment instead of a post?

  • Hi there,

    I’m trying to replace WPs default comment_form and would like to use ACF, specifically acf_form() instead of hacking things up.

    Is this possible or is acf_form() only available for posts?

  • Ok, well after a bit of fiddling I’m guessing acf_form() isn’t going to work because comments aren’t posts (guess sort of obvious now that I think about it).

    Anyways, my issue has changed a bit as I’ve progressed.

    What I’ve done so far

    1) Use ‘Location Rules‘ to target a specific comment form so that I can add in ACF fields

    2) Use the code outlined in the StackOverflow thread titled, How to implement AJAX commenting in WordPress,’ in order to be able to submit my form via AJAX

    What I need help on

    1) How can I get my ACF fields to appear before the comment textarea field?
    …I’ve tried to change both the ‘Positon’ and ‘Order No.’ in my field group settings, but this doesn’t seem to have any effect
    2) When submitting my form, how can I make use of something like the acf/save_post action hook in order to update comment_meta from my ACF field? (I would also like to update the comment’s parent post_meta)

    Thanks in advance!

  • Hi @ryandorn

    To move the field, you need to use Javascript. It should be something like this:

    <script type="text/javascript">
    (function($) {
        
        $(document).ready(function(){
            
            $('.comment-form-comment').before( $('.acf-field-1234567890abc') );
            
        });
        
    })(jQuery);    
    </script>

    Where “.acf-field-1234567890abc” is based on your field key.

    To check if acf/save_post action hook is processing the comment, you can check if the post ID starts with “comment_”. Here’s an example how to do that:

    function my_acf_save_post( $comment_string_id ) {
        
        // Do it only for comment
        if( substr($comment_string_id, 0, 8) === 'comment_' ){
            update_field('field_name', 'the value', $comment_string_id);
        }
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    Also, please check this page to learn how to get the value from a comment field: https://www.advancedcustomfields.com/resources/get-values-comment/.

    I hope this helps ๐Ÿ™‚

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.