使用
3 个回答
- 投票数
-
- 2011-02-21
如果要编辑现有帖子,请尝试使用我的前端编辑器插件.
如果您要创建新帖子,请尝试以下其中一项:
If you want to edit an existing post, try my Front-end Editor plugin.
If you want to create new posts, try one of these:
-
感谢Scribu.您的插件很棒,但不符合我的需求.我正在尝试使用表单编辑现有帖子.我问了一个有关使用前端中的表单编辑用户个人资料的类似问题,并且收到以下答复:http://wordpress.stackexchange.com/questions/9775/how-to-edit-a-user-profile-on-前端这对我来说非常有效.如果有用于编辑帖子的类似解决方案,我将非常感激.Thanks Scribu. Your plugin is fantastic, but it does not fit my need. I am trying to edit an existing post with a form. I asked a similar question about editing a user's profile with a form in the frontend and I received this response: http://wordpress.stackexchange.com/questions/9775/how-to-edit-a-user-profile-on-the-front-end This worked perfectly for me. If there is a similar solution for editing posts I would be so grateful.
- 0
- 2011-02-21
- Carson
-
有可能与之一起添加元框吗?is it possible to add metaboxes along with it??
- 0
- 2011-06-17
- nickfrancis.me
-
@nickfancis.me对此类事情使用注释-这就是它们的作用!@nickfancis.me use comments for this sort of thing - that's what they're there for!
- 0
- 2011-06-17
- TheDeadMedic
-
@nickfrancis.me Metaboxes严格用于后端.也许您是说小部件?@nickfrancis.me Metaboxes are strictly for the backend. Maybe you mean widgets?
- 0
- 2011-06-17
- scribu
-
- 2013-07-15
这是更新帖子/页面的基本解决方案.我添加了自定义元字段的快速演示.这是非常基本的操作,但是可以为您指出前端无帖子式编辑的方向.这不是超级灵活,但是您可以添加所需的任何内容.
将此代码添加到循环中:
<form id="post" class="post-edit front-end-form" method="post" enctype="multipart/form-data"> <input type="hidden" name="post_id" value="<?php the_ID(); ?>" /> <?php wp_nonce_field( 'update_post_'. get_the_ID(), 'update_post_nonce' ); ?> <p><label for="post_title">Title</label> <input type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>" /></p> <p><?php wp_editor( $post->post_content, 'postcontent' ); ?></p> <p><label for="post_title">Test</label> <?php $value = get_post_meta(get_the_ID(), 'edit_test', true); ?> <input type="text" id="edit_test" name="edit_test" value="<?php echo $value; ?>" /></p> <p><label for="post_title">Test 2</label> <?php $value = get_post_meta(get_the_ID(), 'edit_test2', true); ?> <input type="text" id="edit_test2" name="edit_test2" value="<?php echo $value; ?>" /></p> <input type="submit" id="submit" value="Update" /> </form>
然后在页面顶部添加以下代码以处理表单:
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && ! empty($_POST['post_title']) && isset($_POST['update_post_nonce']) && isset($_POST['postcontent']) ) { $post_id = $_POST['post_id']; $post_type = get_post_type($post_id); $capability = ( 'page' == $post_type ) ? 'edit_page' : 'edit_post'; if ( current_user_can($capability, $post_id) && wp_verify_nonce( $_POST['update_post_nonce'], 'update_post_'. $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['edit_test']) ) update_post_meta($post_id, 'edit_test', esc_sql($_POST['edit_test']) ); if ( isset($_POST['edit_test2']) ) update_post_meta($post_id, 'edit_test2', esc_sql($_POST['edit_test2']) ); } else { wp_die("You can't do that"); } }
Here is a basic solutions for updating a post/page. I added a quick demo of custom meta fields. This is pretty basic, but will point you in the direction of plugin-less editing of posts on the front-end. This isn't super flexible, but you can add whatever you need to it.
Add this code into your loop:
<form id="post" class="post-edit front-end-form" method="post" enctype="multipart/form-data"> <input type="hidden" name="post_id" value="<?php the_ID(); ?>" /> <?php wp_nonce_field( 'update_post_'. get_the_ID(), 'update_post_nonce' ); ?> <p><label for="post_title">Title</label> <input type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>" /></p> <p><?php wp_editor( $post->post_content, 'postcontent' ); ?></p> <p><label for="post_title">Test</label> <?php $value = get_post_meta(get_the_ID(), 'edit_test', true); ?> <input type="text" id="edit_test" name="edit_test" value="<?php echo $value; ?>" /></p> <p><label for="post_title">Test 2</label> <?php $value = get_post_meta(get_the_ID(), 'edit_test2', true); ?> <input type="text" id="edit_test2" name="edit_test2" value="<?php echo $value; ?>" /></p> <input type="submit" id="submit" value="Update" /> </form>
Then add this code at the top of the page to process the form:
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && ! empty($_POST['post_title']) && isset($_POST['update_post_nonce']) && isset($_POST['postcontent']) ) { $post_id = $_POST['post_id']; $post_type = get_post_type($post_id); $capability = ( 'page' == $post_type ) ? 'edit_page' : 'edit_post'; if ( current_user_can($capability, $post_id) && wp_verify_nonce( $_POST['update_post_nonce'], 'update_post_'. $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['edit_test']) ) update_post_meta($post_id, 'edit_test', esc_sql($_POST['edit_test']) ); if ( isset($_POST['edit_test2']) ) update_post_meta($post_id, 'edit_test2', esc_sql($_POST['edit_test2']) ); } else { wp_die("You can't do that"); } }
-
我添加了esc_sql().在我看来,您应该避免像这样从公共(半公共)表单提交的数据.I added esc_sql(). It just occurred to me that you should escape data being submitted from a public (semi-public) form like that.
- 0
- 2013-07-16
- Jake
-
谢谢,但是我的表格仅适用于管理员.无论如何,如果我使用此表单进行公开发布,将会很有帮助.Thanks, But my form is only available for Admin. Anyway it will be helpful if ever I use this form for public posting.
- 1
- 2013-07-17
- user1983017
-
- 2013-07-14
最简单的方法是使用带有以下付费扩展名的Ninja Forms之类的东西:
http://wpninjas.com/downloads/front-end-posting/
您也可以自己编写代码.本质上,您将创建一个表单,然后使用
wp_insert_post()
创建完整的帖子.示例表格:
<form action="" id="primaryPostForm" method="POST"> <fieldset> <label for="postTitle"><?php _e('Post Title:', 'framework') ?></label> <input type="text" name="postTitle" id="postTitle" class="required" /> </fieldset> <fieldset> <label for="postContent"><?php _e('Post Content:', 'framework') ?></label> <textarea name="postContent" id="postContent" rows="8" cols="30" class="required"></textarea> </fieldset> <fieldset> <input type="hidden" name="submitted" id="submitted" value="true" /> <button type="submit"><?php _e('Add Post', 'framework') ?></button> </fieldset>
然后在提交时,将其保存如下:
if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) { if ( trim( $_POST['postTitle'] ) === '' ) { $postTitleError = 'Please enter a title.'; $hasError = true; } $post_information = array( 'post_title' => wp_strip_all_tags( $_POST['postTitle'] ), 'post_content' => $_POST['postContent'], 'post_type' => 'post', 'post_status' => 'pending' ); wp_insert_post( $post_information ); }
完整的代码和教程来自: http://wp.tutsplus.com/教程/创意编码/通过前端插入发布/
The easiest way would be to use something like Ninja Forms with the following paid extension:
http://wpninjas.com/downloads/front-end-posting/
You could also code this yourself. Essentially you'll create a form, and then use
wp_insert_post()
to create a full post.A sample form:
<form action="" id="primaryPostForm" method="POST"> <fieldset> <label for="postTitle"><?php _e('Post Title:', 'framework') ?></label> <input type="text" name="postTitle" id="postTitle" class="required" /> </fieldset> <fieldset> <label for="postContent"><?php _e('Post Content:', 'framework') ?></label> <textarea name="postContent" id="postContent" rows="8" cols="30" class="required"></textarea> </fieldset> <fieldset> <input type="hidden" name="submitted" id="submitted" value="true" /> <button type="submit"><?php _e('Add Post', 'framework') ?></button> </fieldset>
and then on submit, you'd save process it something like:
if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) { if ( trim( $_POST['postTitle'] ) === '' ) { $postTitleError = 'Please enter a title.'; $hasError = true; } $post_information = array( 'post_title' => wp_strip_all_tags( $_POST['postTitle'] ), 'post_content' => $_POST['postContent'], 'post_type' => 'post', 'post_status' => 'pending' ); wp_insert_post( $post_information ); }
The full code and tutorial is from: http://wp.tutsplus.com/tutorials/creative-coding/posting-via-the-front-end-inserting/
-
我只想更新(编辑现有的)帖子/页面,而不是插入.I Just want to update (edit existing) the post/page, not insert.
- 0
- 2013-07-14
- user1983017
-
那么,为什么不尝试使用Scribu已引用的"前端编辑器"插件呢?Then why not try the already-referenced "Front End Editor" plugin by Scribu?
- 0
- 2013-07-14
- helgatheviking
我有一个带有标准meta框和一些自定义字段的自定义帖子类型.如何通过前端的表单编辑帖子?