提交后字段数据未显示在前端

时间:2019-05-17 09:25:10

标签: php advanced-custom-fields

我正在提交更新Wordpress ACF field data using the update_field function的表单。效果很好,但我遇到的问题是数据已提交,将其添加到Wordpress中正确的ACF字段中,但随后我手动刷新了页面以查看前端的新数据。

如何提交数据,然后在第一次重新加载时显示?

<form action="#" class="forum-post-comment-form" id="forum-comment-<?php echo $counter; ?>" method="get">
  <input type="text" placeholder="Type your answer here" class="forum-comment-box" id="forum-comment-<?php echo $counter; ?>" name="forum-comment-<?php echo $counter; ?>">
    <span><input type="submit" class="comment-submit-btn" name="submit-<?php echo $counter; ?>" value=""></span>
  </input>
</form>

<?php
$value = array();
if( have_rows( 'comments', $postid ) ):
  while( have_rows( 'comments', $postid ) ): the_row();
    $time_posted = get_sub_field( 'time', $postid );
    $posted = get_sub_field( 'posted_by', $postid );
    $comment = get_sub_field( 'comment', $postid );
    $answerresponse = get_sub_field( 'answer', $postid );

    $value[] = array(
      "time"        => $time_posted,
      "posted_by"   => $posted,
      "comment"     => $comment,
      "answer"          => $answerresponse
    );
  endwhile;
endif;

$submit = "submit-" . $counter;
if(isset($_POST[$submit])){
  $form = "forum-comment-" . $counter;
  $finalanswer = "answer-" . $counter;
  $finalanswer = $_POST[$finalanswer];
  $comment = $_POST[$form];

  //if ($comment != "") {
    $time_posted = date('Y-m-d H:i:s', time());

    // Comments
    $postid = get_the_id();
    $field_key = "field_5c90d272c7ca9";

    $value[] = array(
      "time"          => $time_posted,
      "posted_by"     => $current_user->ID,
      "answer"              => $finalanswer,
      "comment"       => $comment,
    );

    update_field( $field_key, $value, $postid );
  //}
}
?>

ACF字段布局:

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array(
    'key' => 'group_5c90d20a076ab',
    'title' => 'Comments',
    'fields' => array(
        array(
            'key' => 'field_5c90d272c7ca9',
            'label' => 'Comments',
            'name' => 'comments',
            'type' => 'repeater',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'collapsed' => '',
            'min' => 0,
            'max' => 0,
            'layout' => 'block',
            'button_label' => 'Add Comment',
            'sub_fields' => array(
                array(
                    'key' => 'field_5c90f06883914',
                    'label' => 'Time',
                    'name' => 'time',
                    'type' => 'date_time_picker',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array(
                        'width' => '33',
                        'class' => '',
                        'id' => '',
                    ),
                    'display_format' => 'Y-m-d H:i:s',
                    'return_format' => 'Y-m-d H:i:s',
                    'first_day' => 1,
                ),
                array(
                    'key' => 'field_5c90ee4fc7cac',
                    'label' => 'Posted by',
                    'name' => 'posted_by',
                    'type' => 'user',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array(
                        'width' => '33',
                        'class' => '',
                        'id' => '',
                    ),
                    'role' => '',
                    'allow_null' => 0,
                    'multiple' => 0,
                    'return_format' => 'array',
                ),
                array(
                    'key' => 'field_5c90d38cc7cab',
                    'label' => 'Answer?',
                    'name' => 'answer',
                    'type' => 'text',
                    'instructions' => 'Change to "Yes" to mark as the answer',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array(
                        'width' => '33',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => 'No',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'maxlength' => '',
                ),
                array(
                    'key' => 'field_5c90d28ac7caa',
                    'label' => 'Comment',
                    'name' => 'comment',
                    'type' => 'textarea',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array(
                        'width' => '100',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'placeholder' => '',
                    'maxlength' => '',
                    'rows' => '',
                    'new_lines' => '',
                ),
            ),
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'forum',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => array(
        0 => 'excerpt',
        1 => 'discussion',
        2 => 'comments',
        3 => 'slug',
        4 => 'author',
        5 => 'format',
        6 => 'page_attributes',
        7 => 'featured_image',
        8 => 'categories',
        9 => 'tags',
        10 => 'send-trackbacks',
    ),
    'active' => true,
    'description' => '',
));

endif;

1 个答案:

答案 0 :(得分:1)

  

在输出注释数据后,有一个表格(显示在   上面的问题),然后在其下方的update_field函数(也在   上面的问题)

实际上,这里存在问题-您应该在呈现页面之前而不是之后更新字段

工作解决方案

因此,我将表单发布到wp-admin/admin-post.php,这将触发钩子admin_post_{action}(对于已认证的用户)或admin_post_nopriv_{action}(对于未认证的用户)。您可以使用init钩子或任何类似的钩子,但是我认为admin-post.php是处理表单提交的好方法,无论是通过GET还是POST方法进行处理-如果您熟悉wp_ajax_{action},则admin_post_{action} non-AJAX 请求类似。

HTML + PHP表单

<form action="<?php echo admin_url( 'admin-post.php' ); ?>" class="forum-post-comment-form" id="forum-comment-<?php echo $counter; ?>" method="post">
  <input type="text" placeholder="Type your answer here" class="forum-comment-box" id="forum-comment-<?php echo $counter; ?>" name="forum_comment" />

  <!-- I manually added these radios; make sure to edit the code.. -->
  <div>
    Mark as the answer?
    <label><input type="radio" name="answer" value="Yes" /> Yes</label>
    <label><input type="radio" name="answer" value="No" /> No</label>
  </div>

  <!-- Hidden data for add_forum_comment() which updates the field. -->
  <input type="hidden" name="counter" value="<?php echo $counter; ?>" />
  <input type="hidden" name="post_id" value="<?php echo $postid; ?>" />
  <input type="hidden" name="action" value="add-forum-comment" />

  <span><input type="submit" class="comment-submit-btn" name="submit" value=""></span>
</form>

PHP函数,用于处理表单的提交

add_action( 'admin_post_add-forum-comment', 'add_forum_comment' );        // for authenticated users
//add_action( 'admin_post_nopriv_add-forum-comment', 'add_forum_comment' ); // for non-authenticated users
function add_forum_comment() {
    // Check if the post is valid.
    $postid = filter_input( INPUT_POST, 'post_id' );
    if ( ! $post = get_post( $postid ) ) {
        wp_die( 'The post does not exist.' );
    }

    // Check if we've got a valid comment.
    $posted_comment = filter_input( INPUT_POST, 'forum_comment' );
    if ( ! $posted_comment ) {
        // Redirect back to the post.
        wp_redirect( get_permalink( $postid ) );
        exit;
    }


    // Initialize $value and fill it with existing values.
    $value = array();
    if( have_rows( 'comments', $postid ) ):
      while( have_rows( 'comments', $postid ) ): the_row();
        $time_posted = get_sub_field( 'time', $postid );
        $posted = get_sub_field( 'posted_by', $postid );
        $comment = get_sub_field( 'comment', $postid );
        $answerresponse = get_sub_field( 'answer', $postid );

        $value[] = array(
          "time"      => $time_posted,
          "posted_by" => $posted,
          "comment"   => $comment,
          'answer'    => $answerresponse,
        );
      endwhile;
    endif;


    // Now update the ACF field.
    $counter = filter_input( INPUT_POST, 'counter' );
    $finalanswer = filter_input( INPUT_POST, 'answer' );

    $time_posted = date('Y-m-d H:i:s', time());

    // Comments
    $field_key = "field_5c90d272c7ca9";

    $value[] = array(
      "time"      => $time_posted,
      "posted_by" => get_current_user_id(),
      'answer'    => $finalanswer,
      "comment"   => $posted_comment,
    );

    update_field( $field_key, $value, $postid );


    // Redirect back to the post.
    wp_redirect( get_permalink( $postid ) );
    exit;

}

经过测试的工作。而且您不必使用完全相同的代码,但希望该代码对您有所帮助。另外,我使用get_current_user_id()来获取当前用户的ID。