I want to add a front end post editing functionality to one of my Wordpress sites. I found a few plugins that do this, but they don't fits all my needs, so I decided to adapt an existing solution to develop a my own plugin that will return with a shortcode a front end editing form. My plugin works, I can edit and save posts, but I can't solve a warning and I don't have (yet) sufficient knowledge to make this plugin more secure. Any suggestions?
The warning:
Warning: Cannot modify header information - headers already sent by (output started at .../wp-content/plugins/front-post-edit.php:139) in .../wp-includes/pluggable.php on line 1228
P.S. The 139 line is the last line in my plugin (a line without code).
My plugin code:
<?php
/*
* Plugin Name: Front Post Editor
*
*/
add_shortcode( 'front_post_edit', 'post_shortcode' );
function post_shortcode() {
return getForm();
}
function getForm() {
if ( !is_user_logged_in()) {
echo '<p class="alert-box notice">You must be <a href="' . esc_url( wp_login_url( get_permalink() ) ) . '" title="Login">logged in</a>!';
} else {
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "edit_post" && isset($_POST['postid'])) {
$post_to_edit = array();
$post_to_edit = get_post($_POST['postid']);
/* these are the fields that we are editing in the form below */
$title = $_POST['item_title'];
$description = $_POST['item_description'];
$category = $_POST['item_category'];
$location = $_POST['item_location'];
$location2 = $_POST['item_location2'];
/* this code will save the title and description into the post_to_edit array */
$post_to_edit->post_title = $title;
$post_to_edit->post_content = $description;
/* this code is a must */
$pid = wp_update_post($post_to_edit);
/* save taxonomies: post ID, form field name, taxonomy name, if it appends(true) or rewrite(false) */
wp_set_post_terms($pid, array($_POST['item_category']),'category',false);
wp_set_post_terms($pid, array($_POST['item_location']),'location',false);
/* update custom fields with the new info */
update_post_meta($pid, 'item_location2', $location2);
/* redirect user after done editing */
wp_redirect( home_url( '/myposts' ) );
}
/* get post to edit */
$post_to_edit = get_post($_POST['postid']);
/* get this post's category taxonomy term id */
$term_name = strip_tags( get_the_term_list( $post_to_edit->ID, 'category', '', ', ', '' ) );
$term_obj = get_term_by('name', $term_name, 'category');
$term_id = $term_obj->term_id;
/* array for wp_dropdown_category to display with the current post category selected by default */
$args_cat = array(
'selected' => $term_id,
'name' => 'item_category',
'class' => 'postform',
'tab_index' => 10,
'depth' => 2,
'hierarchical' => 1,
'taxonomy' => 'category',
'hide_empty' => false );
/* get this post's location taxonomy term id */
$term_name2 = strip_tags( get_the_term_list( $post_to_edit->ID, 'location', '', ', ', '' ) );
$term_obj2 = get_term_by('name', $term_name2, 'location');
$term_id2 = $term_obj2->term_id;
$args_loc = array(
'selected' => $term_id2,
'name' => 'item_location',
'class' => 'postform',
'tab_index' => 10,
'depth' => 2,
'hierarchical' => 1,
'taxonomy' => 'location',
'hide_empty' => false );
?>
<!-- EDIT FORM -->
<form id="edit_post" name="edit_post" method="post" action="" enctype="multipart/form-data">
<!-- post name -->
<fieldset name="item_title">
<label for="item_title">Item title:</label><br />
<input type="text" id="item_title" value="<?php echo $post_to_edit->post_title; ?>" tabindex="5" name="item_title" />
</fieldset>
<!-- post Content -->
<fieldset class="item_description">
<label for="item_description">Item description:</label><br />
<textarea id="item_description" tabindex="15" name="item_description"><?php echo $post_to_edit->post_content; ?></textarea>
</fieldset>
<!-- post Category -->
<fieldset id="item_category">
<label for="item_category">Item category:</label>
<?php wp_dropdown_categories( $args_cat ); ?>
</fieldset>
<!-- post Location -->
<fieldset id="item_location">
<label for="item_location">Item location:</label>
<?php wp_dropdown_categories( $args_loc ); ?>
</fieldset>
<!-- custom fields -->
<fieldset class="item_location2">
<label for="item_location2">Location 2:</label><br />
<input type="text" value="<?php echo get_post_meta($post_to_edit->ID,'item_location2', true); ?>" id="item_location2" tabindex="20" name="item_location2" />
</fieldset>
<!-- submit button -->
<fieldset class="submit">
<input type="submit" value="Save Post" tabindex="40" id="submit" name="submit" />
</fieldset>
<input type="hidden" name="postid" value="<?php echo $post_to_edit->ID; ?>" /> <!-- DONT REMOVE OR CHANGE -->
<input type="hidden" name="action" value="edit_post" /> <!-- DONT REMOVE OR CHANGE -->
<input type="hidden" name="change_cat" value="" /> <!-- DONT REMOVE OR CHANGE -->
<?php // wp_nonce_field( 'new-post' ); ?>
</form>
<!-- END OF FORM -->
<?php } ?><!-- user is logged in -->
<?php } ?><!-- getForm -->
An Edit button was added to the footer of each entry generated by a loop with this form:
<form class="edit-post" action="<?php echo home_url( '/edit'); ?>" method="post">
<input type="hidden" name="postid" value="<?php the_ID(); ?>" />
<input type="submit" value="Edit" />
</form>
? >
. If there is any space remove it. It will work for you. I have released a plugin before. So i am interested to know about the plugin name. – Kvvaradha Dec 13 '15 at 12:04