How to build a post and comment editing form in a page?

To edit is a bit harder then just creating, but not that hard

first you only display the edit link to the author so you add something like this to your loop:

global $current_user;
get_currentuserinfo();
while (have_posts()) : the_post();
  //regular loop stuff

  //and check if the post author is the current user
  if ($post->post_author = $current_user->ID){
    ehco '<a href="https://wordpress.stackexchange.com/questions/11382/Editpage?qpost_id=".$post->ID.'">Edit</a>';
}
endwhile;

now if you look at that link

ehco '<a href="https://wordpress.stackexchange.com/questions/11382/Editpage?qpost_id=".$post->ID.'">Edit</a>';

you will see that it is looking for a specific page, so create a new page, call it what ever you want and update it in the loop.

now create a new template page
can be an exact copy of your page.php and take out the loop part.
instead enter this code to display the edit form.

<?php
if (isset($_GET['qpost_id'])){
    //check user again
    global $current_user;
    get_currentuserinfo();
    $Qpost = get_post($_GET['qpost_id']);
    if ($current_user->ID = $Qpost->post_author){
        $html="<h1>Edit - ". $Qpost->post_title .'<h1><form name="edit_q" id="edit_q" method="POST" action="">
            <input type="text" class="question-title-box" value="' . $Qpost->post_title; . '" id="question-title-box" name="title" onfocus="if(this.value == \'' . __("Enter Question Title",'qna-forum') . '\'){this.value = \'\';}" onblur="if(this.value == \'\'){this.value = \'' . __("Enter Question Title",'qna-forum') . '\';}" />
            <textarea class="question-box" cols="70" rows="20" id="question-box" name="question" onfocus="if(this.value == \'' . __("Enter Your Question Here",'qna-forum') . '\'){this.value = \'\';}" onblur="if(this.value == \'\'){this.value = \'' .__("Enter Your Question Here",'qna-forum') . '\';}">' . $Qpost->post_content . '</textarea>';
        $html  .= "<div class="question-form-bottom">".__('Category','qna-forum').":<select name="category" id='category'>";
        $categories = get_categories(array(
                                        'type' => 'post',
                                        'orderby' => 'count',
                                        'order'=> 'DESC',
                                        'hide_empty'=>0
                                    ));
        foreach($categories as $cat){
            if(get_option('q_cat_' . $cat->term_id) == "TRUE"){
                $html .= '<option value="' . $cat->term_id . '">' . $cat->cat_name . '</option>';
            }
        }
        $html .= "</select>";
        $html .= "<input type="hidden" value="" . wp_create_nonce( "edit_q_question_form' ) . "' name="nonce" />";
        $html .="<input type="hidden" name="action" value="edit_q_ask_question" />";
        $html .="<input type="hidden" name="q_to_update" value="".$qpost->ID."" />";
        $html .="<input type="submit" name="submit" value="submit" /></form>";
        echo $html;
    }
}
?>  

as you can see most of it is taken from the plugin, i just edited it a bit to fit your needs.

next you need to update the post and to do so you can use wp_update_post()
so add this code just above the code you’ve just added:

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "edit_q_ask_question") {
// Do some minor form validation and make sure there is content
    $nonce=$_REQUEST['nonce'];
    if (! wp_verify_nonce($nonce, 'edit_q_question_form') ) die('Security check'); 
    if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
    } else {
        echo 'Please enter a  title';
    }
    if (isset ($_POST['question'])) {
        $question = $_POST['question'];
    } else {
        echo 'Please enter some content';
    }
    $new_question = array(
        'post_title'    => $title,
        'post_content'  => $question,
        'post_category' => array($_POST['category'])
        'ID' => $_POST['q_to_update'];
        );

    // Update the post into the database
    $new_id = wp_update_post( $new_question );
    echo 'Qestion updated and you can see it <a href="'.get_permalink($new_id).'">here</a>';
}

I would probably add some Security check and data validation its not perfect but its a very nice start.

Leave a Comment