5

I am making front end admin for users to add/edit their posts. I already made post add form and it works, but edit form doesnt work.

functions.php

function add_new_post( $post_id )
{
    if( $post_id == 'new' ) {
        // Create a new post
        $post = array(
            'post_title' => $_POST["fields"]['field_52c810cb44c7a'],
            'post_category' => array(4),
            'post_status'  => 'draft',
            'post_type'  => 'post'
        );

        // insert the post
        $post_id = wp_insert_post( $post );

        return $post_id;
    }
    else {
        return $post_id;
    }
}add_filter('acf/pre_save_post' , 'add_new_post' );

index.php

<div id="updateform-<?php the_ID(); ?>" class="collapse">
    <?php
    echo get_the_ID();
    $args = array(
        'post_id' => get_the_ID(), // post id to get field groups from and save data to
        'field_groups' => array(31), // this will find the field groups for this post (post ID's of the acf post objects)
        'form' => true, // set this to false to prevent the <form> tag from being created
        'form_attributes' => array( // attributes will be added to the form element
            'id' => 'post',
            'class' => '',
            'action' => get_permalink( get_the_ID() ),
            'method' => 'post',
        ),
        'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
        'html_before_fields' => '', // html inside form before fields
        'html_after_fields' => '', // html inside form after fields
        'submit_value' => 'Update', // value for submit field
        'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
    );
    acf_form( $args );
    ?>
</div>
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Rokas
  • 1,712
  • 2
  • 18
  • 35
  • did you ever get this working? I am looking for a solution to this question as well. – Derek Mar 08 '15 at 17:17
  • Yes, but it was long time ago. Now ACF has built in function to output their form to frontend. Please check http://www.advancedcustomfields.com/resources/create-a-front-end-form/ – Rokas Mar 09 '15 at 13:56

3 Answers3

1

For saving posts you should use save_post not pre_save_post. Basically pre_save_post is used for new posts.

Use this below add_filter('save_post' , 'add_new_post');

payo
  • 4,501
  • 1
  • 24
  • 32
0

I created plugins for that:

  1. Forms actions https://wordpress.org/plugins/forms-actions/

Add actions to yours ACF forms.

  1. ACF Frontend display https://wordpress.org/plugins/acf-frontend-display/

Display your ACF form on frontend

0

If by some chance you happen to be using Elementor Page Builder, you can do this by installing Advanced Widgets for Elementor. It comes with an ACF Form widget that you can just drag & drop anywhere into your site and configure your form through the UI (no coding required).

mae
  • 14,947
  • 8
  • 32
  • 47