admin管理员组文章数量:1406052
I have learned how to create a post from the frontend but how about editing it ? This is the code I am trying to create a simple frontend form for posting
Thank you
I have learned how to create a post from the frontend but how about editing it ? This is the code I am trying to create a simple frontend form for posting
Thank you
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked May 16, 2011 at 14:32 EnexoOnomaEnexoOnoma 2213 gold badges6 silver badges16 bronze badges2 Answers
Reset to default 4Just like the example you linked, but instead of using wp_insert_post()
you use: wp_update_post()
so your form becomes:
<?php
$post_to_edit = get_post($post_id);
?>
<!-- edit Post Form -->
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="">
<p>
<label for="title">Title</label><br />
<input type="text" id="title" value="<?php echo $post_to_edit->post_title; ?>" tabindex="1" size="20" name="title" />
</p>
<p>
<label for="description">Description</label><br />
<textarea id="description" tabindex="3" name="description" cols="50" rows="6"><?php echo $post_to_edit->content; ?></textarea>
</p>
<p>
<?php
$cat = wp_get_post_terms( $post_to_edit->ID, 'category');
wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=categoryselected='.$cat[0]->term_id);
?>
</p>
<p>
<label for="post_tags">Tags</label>
<input type="text" value="<?php the_terms( $post_to_edit->ID, 'post_tag', '', ', ', '' ); ?>" tabindex="5" size="16" name="post_tags" id="post_tags" />
</p>
<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>
<input type="hidden" name="action" value="f_edit_post" />
<input type="hidden" name="pid" value="<?php echo $post_to_edit->ID; ?>" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
</div>
<!--// edit Post Form -->
and the processing becomes:
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "f_edit_post" && isset($_POST['pid'])) {
//get the old post:
$post_to_edit = get_post((int)$_POST['pid']);
//do you validation
//...
//...
// Add the content of the form to $post_to_edit array
$post_to_edit['post_title'] = $_post['title'];
$post_to_edit['post_content'] = $_post['description'];
$post_to_edit['tags_input'] = array($_post['post_tags']);
//save the edited post and return its ID
$pid = wp_update_post($post_to_edit);
//set new category
wp_set_post_terms($pid,(array)($_POST['cat']),'category',true);
}
now this has no validation so i leave it up to you.
Scribu Front End Editor havent tried it personally but i have read good stuff about it
本文标签: front endHow can I edit a post from the frontend
版权声明:本文标题:front end - How can I edit a post from the frontend? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744956786a2634403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论