Rokas
Rokas

Reputation: 1712

Update wordpress post from front-end (using acf)

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>

Upvotes: 5

Views: 7235

Answers (3)

mae
mae

Reputation: 15656

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).

Upvotes: 0

user3719894
user3719894

Reputation: 11

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

Upvotes: 0

Ravi Shanker SIPL
Ravi Shanker SIPL

Reputation: 11

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');

Upvotes: 1

Related Questions