Support

Account

Home Forums Front-end Issues acf_form without loggin in

Solving

acf_form without loggin in

  • We currently use acf_form to have an front-end form where people can create a new post.
    We want this to be done anonymously, without people having to log in.

    Right now, u háve to log in, otherwise the script fails and it won’t do anything after submitting.

    Is there a way to let people post something without loggin in on the front-end? Or is there a way we can force to say it has to be added to the user admin?

    Hope someone on the forum has any idea.

    Thanks

  • Hiya @jeffer

    Posts always need to be assigned to an author. So what I’d suggest is create a user in your admin to whom these posts will be assigned – Anon User – or whatever you’d like to call them. Remember to make sure they’re of the correct role to publish posts.

    Then in your acf_form function, specify this user to be the author of that post. In the args for your form, just add 'post_author' => 123 – where 123 is the ID of the user you’ve created.

  • Hi @markbloomfield,

    Thanks for your reply. Unfortunately, I already tried it like that, and now again, but it still fails when a user is not logged in.

    I get redirected back to the same page I already am on as soon as I use post_author in the array.

    `<?php

    $new_post = array(
    ‘post_id’ => ‘new’,
    ‘post_author’ => ‘9’,
    ‘field_groups’ => array(2023,2028),
    ‘form’ => true,
    ‘return’ => ‘%post_url%’, // Redirect to new post url
    ‘html_before_fields’ => ”,
    ‘html_after_fields’ => ”,
    ‘submit_value’ => ‘Melding plaatsen’,
    ‘updated_message’ => ‘Melding geplaatst!’
    );
    acf_form( $new_post );

    ?>

  • Hello,

    Just letting you know, I also tried this code, which I took from the acf website:

    
    acf_form(array(
      'post_id'  => 'new_post',
      'post_title' => true,
      'post_content' => true,
      'new_post'  => array(
       'post_type'  => 'hufter',
       'post_author' => 9,
       'post_status' => 'publish'
      ),
      'return'  => home_url('contact-form-thank-you'),
      'submit_value' => 'Send'
     ));
    

    (hufter is the post_type we use)

    But even that is sending me back to the page I already am on, when I am not logged in, and when I am logged in; it does save the post but it does not assign it to the user (even though that user is set as admin so he can publish)

  • Have you tried creating your own acf/pre_save_post filter? https://www.advancedcustomfields.com/resources/acf-pre_save_post/

    In this function, when setting the post values to insert the post you can set the ‘post_author’ value.

  • Hello John,

    Actually yes, we have that.
    I even tried to take a clean code now, from the link you sent, and added post_author, but even that does not seem to work, we keep getting returned to the form when not logged in

    function my_pre_save_post( $post_id ) {
    
        // check if this is to be a new post
        if( $post_id != 'new' ) {
    
            return $post_id;
    
        }
    
        // Create a new post
        $post = array(
            'post_status'  => 'draft' ,
            'post_author' => '9',
            'post_title'  => 'A title, maybe a dadadada variable' ,
            'post_type'  => 'hufter' ,
        );  
    
        // insert the post
        $post_id = wp_insert_post( $post ); 
    
        // return the new ID
        return $post_id;
    
    }
    
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
    
  • Hello,

    I am completely stupid :). The issue has been solved.
    I overlooked a function which had a return when a user is not logged in..

    add_action( 'get_header', 'tsm_do_acf_form_head', 1 );
    function tsm_do_acf_form_head() {
     // Bail if not logged in or not able to post
     if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
     return;
     }
     acf_form_head();
    }

    Thank you very much for your help, above code was the issue and I removed it.

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

The topic ‘acf_form without loggin in’ is closed to new replies.