【问题标题】:Update wordpress post from front-end (using acf)从前端更新 wordpress 帖子(使用 acf)
【发布时间】:2014-01-29 10:03:48
【问题描述】:

我正在为用户创建前端管理员以添加/编辑他们的帖子。我已经制作了帖子添加表单,它可以工作,但编辑表单不起作用。

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>

【问题讨论】:

标签: wordpress advanced-custom-fields


【解决方案1】:

要保存帖子,您应该使用save_post 而不是pre_save_post。 基本上pre_save_post 用于新帖子。

在下面使用add_filter('save_post' , 'add_new_post');

【讨论】:

    【解决方案2】:

    我为此创建了插件:

    1. 表单操作 https://wordpress.org/plugins/forms-actions/

    向您的 ACF 表单添加操作。

    1. ACF 前端显示 https://wordpress.org/plugins/acf-frontend-display/

    在前端显示您的 ACF 表单

    【讨论】:

      【解决方案3】:

      如果您碰巧使用了Elementor Page Builder,您可以通过安装Advanced Widgets for Elementor 来做到这一点。它带有一个 ACF Form 小部件,您可以将其拖放到您网站的任何位置并通过 UI 配置您的表单(无需编码)。

      【讨论】:

        猜你喜欢
        • 2020-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-14
        • 2022-11-24
        • 1970-01-01
        相关资源
        最近更新 更多