具有多个帖子的前台表单

时间:2018-07-02 作者:Jillian Hoenig

我一直在研究前端后期编辑/创建表单,但我似乎找不到任何关于在同一页面上有多个表单的信息。请原谅我对POST方法的理解不准确。

这是一个查询当前用户的所有草稿的页面post_type 机密的然后它遍历所有帖子,并为每个帖子创建一个编辑表单。

<?php
/**
 * Template Name: Edit Post
 *
 * This is the default template.  It is used when a more specific template can\'t be found to display
 * posts.  It is unlikely that this template will ever be used, but there may be rare cases.
 */
get_header(); // Loads the header.php template.

do_action( \'before_content\' ); // supreme_before_content
do_action( \'templ_before_container_breadcrumb\' );

?>
<title>Edit your classifieds</title>
<section id="content" class="large-9 small-12 columns">
<?php do_action( \'open_content\' ); ?>

<?php
$user = wp_get_current_user()->data;
$user_id = $user->ID;

$query = new WP_Query(array(
    \'post_type\' => \'classified\',
    \'post_status\' => \'draft\',
    \'author\' => $user_id,
    // \'posts_per_page\' => -1
));
echo "<h1>Edit your pending classifieds</h1><br>";
echo "<p>The following posts are your pending classifieds. After you edit a classified, it will be reviewed by our team before publishing.</p>";
?>
<div class="accordion" id="post-listing"><?php
    while ($query->have_posts()) {

        $query->the_post();

        // process form
        // if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] ) {
        if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && ! empty($_POST[\'post_id\']) && ! empty($_POST[\'post_title\']) && isset($_POST[\'postcontent\']) ) {

            header(\'Location: /\');

            $post_id = $post->ID;
            $post = array(
                \'ID\'             => esc_sql($post_id),
                \'post_content\'   => esc_sql($_POST[\'postcontent\']),
                \'post_title\'     => esc_sql($_POST[\'post_title\'])
            );
            wp_update_post($post);
            ?><pre><?php var_dump($_POST);?></div>pre><?php
            if ( isset($_POST[\'owner_name\']) ) update_post_meta($post_id, \'owner_name\', esc_sql($_POST[\'owner_name\']) );
            if ( isset($_POST[\'phone\']) ) update_post_meta($post_id, \'phone\', true );
            if ( isset($_POST[\'address\']) ) update_post_meta($post_id, \'address\', true );
            if ( isset($_POST[\'zip_code\']) ) update_post_meta($post_id, \'zip_code\', true );
            if ( isset($_POST[\'add_coupon\']) ) update_post_meta($post_id, \'add_coupon\', true );
        }


        wp_nonce_field( \'update_post_\'. get_the_ID(), \'update_post_nonce\' );


        $fields = get_post_meta($post->ID);

        $name    = $fields["owner_name"][0];
        $phone   = $fields["phone"][0];
        $address = $fields["address"][0];
        $zip     = $fields["zip_code"][0];
        $coupon  = $fields["add_coupon"][0];
        ?>
        <div class="accordion-navigation step-wrapper">
            <a class="step-heading" href="#" onclick="toggle(edit_post_<?php echo $post->ID; ?>);"><span style="float: none;max-width: unset;text-align: left;padding-left: 26px;"><h4 style="margin: 0">Edit <?php echo $post->post_title; ?></h4></span></a>
            <div class="step-post content  clearfix" id="edit_post_<?php echo $post->ID; ?>" href="#">
                <form id="post" class="dropzone form_front_style" method="post" enctype="multipart/form-data">

                    <input type="hidden" name="post_id" value="<?php the_ID(); ?>" />
                    <div class="step-post content  clearfix current">
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="post_title">Title</label>
                        <input class="textfield" type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>" /></div>

                        <h3>Classified Information</h3>
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="post_title">Detail</label>
                        <span style="color: #333"><?php wp_editor( $post->post_content, \'postcontent\' ); ?></span></div>

                        <h3>Seller Contact Information</h3>
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="post_title">Owner Name</label>
                        <input class="textfield" type="text" id="name" name="owner_name" value="<?php echo $name; ?>" /></div>

                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="post_title">Phone</label>
                        <input class="textfield" type="text" id="phone" name="phone" value="<?php echo $phone; ?>" /></div>

                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="post_title">Address</label>
                        <input class="textfield" type="text" id="address" name="address" value="<?php echo $address; ?>" /></div>

                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="post_title">Zip</label>
                        <input class="textfield" type="text" id="zip_code" name="zip_code" value="<?php echo $zip; ?>" /></div>

                        <h3>Coupons</h3>
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="post_title">Add Coupon</label>
                        <input class="textfield" <?php echo $coupon ? "disabled style=\'background-color:#f8f8f8;color:#bdbcbc;\'" : ""; ?> type="text" id="add_coupon" name="add_coupon" value="<?php echo $coupon; ?>" /></div>

                        <input class="textfield" type="submit" id="submit" value="Update" />
                    </div>
                </form>
            </div>
        </div>
    <?php
    }
    ?>
</div>
</section>
<!-- #content -->
<?php do_action( \'after_content\' );
$page_for_posts = get_option( \'page_for_posts\' );

if ( $page_for_posts != \'\' ) {
   apply_filters( \'supreme-post-listing-sidebar\',supreme_post_listing_sidebar() 
);// load the side bar of listing page
} else {    
  get_sidebar();
}
get_footer();
?>

<script>
// open accordion when anchor link directly above is clicked
function toggle(target) {
  var   i,
  active_elements = document.getElementsByClassName("active"),
  clicked = document.getElementById(target[\'id\']);

  if (!clicked.classList.contains("active")) {
    for (i = 0; i < active_elements.length; i++) {
        active_elements[i].classList.remove("active");
    }
  }
  clicked.classList.toggle("active");
}
</script>
表单似乎正在运行,但当用户更新post_titlepost_content, 所有帖子都会使用该值进行更新。

(此外,元字段-phone, address, etc-根本不更新..)

感谢您的帮助!

1 个回复
最合适的回答,由SO网友:Jillian Hoenig 整理而成

发生的事情是,全局变量$post 没有出现在帖子页面上-因此在下面的条件中,表单正在处理,

if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && ! empty($_POST[\'post_id\']) && ! empty($_POST[\'post_title\']) && isset($_POST[\'postcontent\']) ) {

        header(\'Location: /\');

        $post_id = $post->ID;
        $post = array(
            \'ID\'             => esc_sql($post_id),
            \'post_content\'   => esc_sql($_POST[\'postcontent\']),
            \'post_title\'     => esc_sql($_POST[\'post_title\'])
        );
        wp_update_post($post);

        if ( isset($_POST[\'owner_name\']) ) update_post_meta($post_id, \'owner_name\', esc_sql($_POST[\'owner_name\']) );
        if ( isset($_POST[\'phone\']) ) update_post_meta($post_id, \'phone\', true );
        if ( isset($_POST[\'address\']) ) update_post_meta($post_id, \'address\', true );
        if ( isset($_POST[\'zip_code\']) ) update_post_meta($post_id, \'zip_code\', true );
        if ( isset($_POST[\'add_coupon\']) ) update_post_meta($post_id, \'add_coupon\', true );
    }
$post_id 计算结果为null。

我的解决方案是从$_POST 对象以下是修订后的代码:

    if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && ! empty($_POST[\'post_id\']) && ! empty($_POST[\'post_title\']) && isset($_POST[\'postcontent\']) ) {

            header(\'Location: /\');

            $post_id = $post->ID;
            $post = array(
                \'ID\'             => $_POST[\'post_id\'], // use $_POST object instead of global $post
                \'post_content\'   => esc_sql($_POST[\'postcontent\']),
                \'post_title\'     => esc_sql($_POST[\'post_title\'])
            );

            wp_update_post($post);


            if ( isset($_POST[\'owner_name\']) ) update_post_meta($_POST[\'post_id\'], \'owner_name\', esc_sql($_POST[\'owner_name\']) );
            if ( isset($_POST[\'phone\']) ) update_post_meta($_POST[\'post_id\'], \'phone\', esc_sql($_POST[\'phone\']) );
            if ( isset($_POST[\'address\']) ) update_post_meta($_POST[\'post_id\'], \'address\', esc_sql($_POST[\'address\']) );
            if ( isset($_POST[\'zip_code\']) ) update_post_meta($_POST[\'post_id\'], \'zip_code\', esc_sql($_POST[\'zip_code\']) );
            if ( isset($_POST[\'add_coupon\']) ) update_post_meta($_POST[\'post_id\'], \'add_coupon\', esc_sql($_POST[\'add_coupon\']) );
        }
和全文供参考

<?php
/**
 * Template Name: Edit Post
 *
 * This is the default template.  It is used when a more specific template can\'t be found to display
 * posts.  It is unlikely that this template will ever be used, but there may be rare cases.
 */
get_header(); // Loads the header.php template.

do_action( \'before_content\' ); // supreme_before_content
do_action( \'templ_before_container_breadcrumb\' );

?>
<title>Edit your classifieds</title>
<section id="content" class="large-9 small-12 columns">
<?php do_action( \'open_content\' ); ?>

<?php
$user = wp_get_current_user()->data;
$user_id = $user->ID;

$query = new WP_Query(array(
    \'post_type\' => \'classified\',
    \'post_status\' => \'draft\',
    \'author\' => $user_id,
    // \'posts_per_page\' => -1
));
echo "<h1>Edit your pending classifieds</h1><br>";
echo "<p>The following posts are your pending classifieds. After you edit a classified, it will be reviewed by our team before publishing.</p>";
?>
<div class="accordion" id="post-listing"><?php
    while ($query->have_posts()) {

        $query->the_post();

        // process form
        // if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] ) {
    if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && ! empty($_POST[\'post_id\']) && ! empty($_POST[\'post_title\']) && isset($_POST[\'postcontent\']) ) {

            // header(\'Location: /\');

            $post_id = $post->ID;
            $post = array(
                \'ID\'             => $_POST[\'post_id\'],
                \'post_content\'   => esc_sql($_POST[\'postcontent\']),
                \'post_title\'     => esc_sql($_POST[\'post_title\'])
            );

            wp_update_post($post);
            // var_dump($post);
            // var_dump($post_id);

            if ( isset($_POST[\'owner_name\']) ) update_post_meta($_POST[\'post_id\'], \'owner_name\', esc_sql($_POST[\'owner_name\']) );
            if ( isset($_POST[\'phone\']) ) update_post_meta($_POST[\'post_id\'], \'phone\', esc_sql($_POST[\'phone\']) );
            if ( isset($_POST[\'address\']) ) update_post_meta($_POST[\'post_id\'], \'address\', esc_sql($_POST[\'address\']) );
            if ( isset($_POST[\'zip_code\']) ) update_post_meta($_POST[\'post_id\'], \'zip_code\', esc_sql($_POST[\'zip_code\']) );
            if ( isset($_POST[\'add_coupon\']) ) update_post_meta($_POST[\'post_id\'], \'add_coupon\', esc_sql($_POST[\'add_coupon\']) );
        }


        wp_nonce_field( \'update_post_\'. get_the_ID(), \'update_post_nonce\' );


        $fields = get_post_meta($post->ID);

        $name    = $fields["owner_name"][0];
        $phone   = $fields["phone"][0];
        $address = $fields["address"][0];
        $zip     = $fields["zip_code"][0];
        $coupon  = $fields["add_coupon"][0];
        ?>
        <!-- <pre><?// php var_dump($post->ID);?></pre> -->
        <div class="accordion-navigation step-wrapper">
            <a class="step-heading" href="#" onclick="toggle(edit_post_<?php echo $post->ID; ?>);"><span style="float: none;max-width: unset;text-align: left;padding-left: 26px;"><h4 style="margin: 0">Edit <?php echo $post->post_title; ?></h4></span></a>
            <div class="step-post content  clearfix" id="edit_post_<?php echo $post->ID; ?>" href="#">
                <div class="step-post content  clearfix current">
                    <form id="post" class="dropzone form_front_style" method="post" enctype="multipart/form-data">

                    <input type="hidden" name="post_id" value="<?php the_ID(); ?>" />
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="post_title">Title</label>
                        <input class="textfield" type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>" /></div>

                        <h3>Classified Information</h3>
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="postcontent">Detail</label>
                        <span style="color: #333"><?php wp_editor( $post->post_content, \'postcontent\' ); ?></span></div>

                        <h3>Seller Contact Information</h3>
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="name">Owner Name</label>
                        <input class="textfield" type="text" id="name" name="owner_name" value="<?php echo $name; ?>" /></div>

                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="phone">Phone</label>
                        <input class="textfield" type="text" id="phone" name="phone" value="<?php echo $phone; ?>" /></div>

                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="address">Address</label>
                        <input class="textfield" type="text" id="address" name="address" value="<?php echo $address; ?>" /></div>

                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="zip_code">Zip</label>
                        <input class="textfield" type="text" id="zip_code" name="zip_code" value="<?php echo $zip; ?>" /></div>

                        <h3>Coupons</h3>
                        <div class="form_row clearfix custom_fileds"><label class="r_lbl" for="add_coupon">Add Coupon</label>
                        <input class="textfield" <?php echo $coupon ? "disabled style=\'background-color:#f8f8f8;color:#bdbcbc;\'" : ""; ?> type="text" id="add_coupon" name="add_coupon" value="<?php echo $coupon; ?>" /></div>

                        <input class="textfield" type="submit" id="submit" value="Update" />
                    </div>
                </form>
            </div>
        </div>
        <?php
        }
        ?>
    </div>
</section>
<!-- #content -->
<?php do_action( \'after_content\' );
$page_for_posts = get_option( \'page_for_posts\' );

if ( $page_for_posts != \'\' ) {
    apply_filters( \'supreme-post-listing-sidebar\',supreme_post_listing_sidebar() );// load the side bar of listing page
} else {    
    get_sidebar();
}
get_footer();
?>
<script>
// open accordion when anchor link directly above is clicked
function toggle(target) {
    var i,
    active_elements = document.getElementsByClassName("active"),
    clicked = document.getElementById(target[\'id\']);

    if (!clicked.classList.contains("active")) {
        for (i = 0; i < active_elements.length; i++) {
            active_elements[i].classList.remove("active");
            console.log("removed");
        }
    }
    clicked.classList.toggle("active");
}
</script>

结束

相关推荐

Form Object Gravity Forms

重力形式。我尝试在渲染表单字段之前对其进行操作add_filter(\"gform_pre_render\", \"my_function\", 10, 5); function my_function($form){ ... $form[\"fields\"][0][\"content\"] = \'This is a html-block\' } 这样,我可以传递html块的内容,假设html是表单上的第一个字段。如何通过id? 假设上面的html块