从前端表格过账的审查

在过去的三天里,我们完成了不少工作。首先,我们建立了一个表单,从前端发布。一旦我们设置好了表单,我们就添加了一些代码,以允许图像与表单一起上传。最后,我们添加了在表单上使用自定义元框的功能,以收集自定义字段中的其他信息,供我们使用。这是很多代码。另外,我在编写代码时遇到了一些困难,这些代码有时有效,有时无效。我都修好了。我认为最好把我所有的代码都放在一个帖子里,这样我们就可以一起复习了。

让我们看看代码。现在我只想把我这里的东西给你看看。我不会花太多时间解释它。如果你有问题,你可以随时回到上面链接的原始帖子。首先,让我们看看主表单页面。现在请记住,我使用了一个2010儿童主题。所以我的代码将反映这一点。根据您使用的主题,您的可能略有不同。如果你还记得我制作了一个页面模板,保存在一个基本循环中,并将其添加到我的表单和处理代码中。以下是完整的最终结果:

<?php(电话)/*模板名称:评分葡萄酒表格*/?><?php(电话)if('POST'==$_SERVER['REQUEST_METHOD']&&!空($_POST['action'])&&$_POST['action']==“new_POST”){//进行一些次要的表单验证以确保有内容if(isset($_POST['title'])){$title=$_POST[“标题”];}其他{echo“请输入葡萄酒名称”;}if(isset($_POST['description'])){$description=$_POST['description'];}其他{echo“请输入一些注释”;}$tags=$_POST['POST_tags'];$winerating=$_POST['winerating'];//将表单输入添加到$new_post数组$new_post=数组(“post_title”=>$title,“post_content”=>$description,“post_category”=>数组($_post['cat']),//也可用于自定义分类法“tags_input”=>数组($tags),'post_status'=>'publish',//选择:publish、preview、future、draft等。“post_type”=>“post”,//'post',page'或使用自定义帖子类型“葡萄酒评级”=>$葡萄酒评级);//保存帖子$pid=wp_insert_post($new_post);//将我们的商品标签作为单独标签wp_set_post_tags($pid,$_post['post_tags']);//重新定向到新的保存帖子$link=获取permalink($pid);wp_redirect($link);//添加我们的自定义字段add_post_meta($pid,‘rating’,$winerating,true);//插入我们的媒体附件if($_FILES){foreach($_FILES作为$file=>$array){$newupload=插入附件($file,$pid);//$newupload返回文件的附件id//刚刚上传。现在就用它做你想做的事。}}//结束文件的IF语句}//结束以整个表单开头的IF语句//张贴帖子do_action(“wp_insert_post”,“wp_ insert_ppost”);?><?php get_header();?><div id=“container”><div id=“content”role=“main”><?php if(have_posts())while(haver_posts)):the_post();?><div id=“post-<?php the_id();?>”?php post_class();?>><?php if(is_front_page()){?><h2 class=“entry-title”><?php the_title();?></氢气><?php}其他{?><h1 class=“entry-title”><?php the_title();?></h1><?php}?><div class=“form-content”><?php the_content();?><!-- 葡萄酒评级表--><div class=“wpcf7”><form id=“new_post”name=“new_pos”method=“post”action=“”class=“wpcf7-form”enctype=“multipart/form-data”><!-- 帖子名称--><fieldset name=“name”>葡萄酒名称:<input type=“text”id=“title”value=“”tabindex=“5”name=“title“/></fieldset><!-- 帖子类别--><fieldset class=“category”>类型:<?php wp_dropdown_categories('tab_index=10&taxonomy=category&hide_empty=0');?></fieldset><!-- 发布内容--><fieldset class=“content”>说明和注释:<textarea id=“description”tabindex=“15”name=“descriptions”cols=“80”rows=“10”></textarea></fieldset><!-- 葡萄酒评级--><fieldset class=“葡萄酒评级”><label for=“winerating”>您的评级<input type=“text”value=“”id=“winerating”tabindex=“20”name=“wineration”/></fieldset><!-- 图像--><fieldset class=“images”><label for=“bottle_front”>瓶子前面<input type=“file”name=“bottle_front”id=“bottlete_front“tabindex=”25“/></fieldset><fieldset class=“images”><label for=“bottle_rer”>瓶子背面<input type=“file”name=“bottle_rer”id=“bottlet_rer“tabindex=”30“/></fieldset><!-- 帖子标签--><fieldset class=“tags”>其他关键字(逗号分隔):<input type=“text”value=“”tabindex=“35”name=“post_tags”id=“post_tags”/></fieldset><fieldset class=“submit”><input type=“submit”value=“Post Review”tabindex=“40”id=“submite”name=“subment”/></fieldset><input type=“hidden”name=“action”value=“new_post”/><?php wp_nonce_field(“新发布”);?></form></div><!--结束WPCF7--><!-- 表格结束--><?php wp_link_pages(数组('before'=>'<div class=“page-link”>'.__('pages:','twentyten'),'after'=>'</div>');?><?php edit_post_link(__('编辑','twentyten'),'<span class=“edit-link”>','</span>');?></div><!--。条目内容--></div><!--#发布-##--><?php comments_template(“”,true);?><?php endwhile;//循环结束。?></div><!--#内容--></div><!--#容器--><?php get_sidebar();?><?php get_footer();?>

因此,该表单中包含所有三个教程的代码。它包含前端表单的基本帖子。它具有允许上传图像的代码。它还包含自定义字段的元框代码。我们需要的下一段代码位于functions.php中。这是允许上传图像的位。

函数insert_attachment($file_handler,$post_id,$setthumb='false'){//检查以确保上传成功if($_FILES[$file_handler]['error']!==UPLOAD_ERR_OK)__return_false();require_once(ABSPATH.“wp-admin”.'/includes/image.php');require_once(ABSPATH.“wp-admin”.'/includes/file.php');require_once(ABSPATH.“wp-admin”.'/includes/media.php');$attach_id=media_handle_upload($file_handler,$post_id);如果($setthumb)更新post_meta($post_id,'thumbnail_id',$attach_id);返回$attach_id;}

这就是发布部分。我会再添加一些代码,这些代码是你在表单中显示额外内容所需要的。例如,我表单中的图像会上传并附加到帖子中,最后一张图像会成为特色图像。但贴在帖子上的图像没有设置为显示。以下是循环中用于实际显示图像的代码:

<div class=“wineBottles”><?php(电话)$args=数组(“post_type”=>“attachment”,“numberposts”=>-1,“post_status”=>null,“post-parent”=>$post->ID);$attachments=get_posts($args);if($附件){foreach($附件作为$附件){?><div class=“wineImage”><span class=“wineTitle”><?php echo apply_filters('the_title',$attachment->post_title);?></span><?php the_attachment_link($attachment->ID,false);?></div><?php}}?></div>

正如我之前所说,这里添加了一些不同的css选择器。您需要将它们替换掉,或在您的css中定义它们。我不会在本教程中涉及到造型方面的内容,因为这是个人喜好的问题。最后,我们需要一段代码来显示表单中自定义字段中的数据。这也会出现在您的循环中,无论您想在哪里输出数据。无论您需要什么数据,您都需要对其进行更改,这是一个非常基本的示例:

<div class=“wineRating”><?php if(get_post_meta($post->ID,“rating”,true)):?><?php echo get_post_meta($post->ID,“rating”,true);?><?php-endif;?></div>

这就是全部。到目前为止,我使用的每一段代码都是为了让我的帖子从前端表单正常工作。我希望这对你有所帮助。现在,拿着它跑吧,一旦你明白了这一切是如何运作的,你就可以做很多事情了。

更新:@trusktr在wordpress答案网站上留言,帮了我一把。我还没有机会玩这个,但想尽快把它放在这里,供用户使用。Trusktr报告说,在顶部的验证代码中发生了变化:

isset($_POST[“说明”])$_POST['description']!=''

解决我们的问题,并在不应该发布表单时阻止其发布。如果有人对此感到困惑,请告诉我们结果如何!