Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This plugin enables you to submit a custom post type called 'Questions' using a form that can be embeded in a page via shortcode.

Is there a way of accomplishing the same, but this time, building a form and embedding it on a page to edit this custom post type and comments? (it doesn't have to be a shortcode).

Reference picture:

enter image description here

share|improve this question
    
to edit or create? –  Bainternet Mar 7 '11 at 17:42
    
@Bainternet To edit, because with that plugin you can already create 'Questions' (custom post type). –  janoChen Mar 7 '11 at 17:57
add comment

1 Answer

up vote 3 down vote accepted

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="Editpage?qpost_id='.$post->ID.'">Edit</a>';
}
endwhile;

now if you look at that link

ehco '<a href="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.

share|improve this answer
    
Thanks a lot! I've been asking this for weeks. I will study the code. –  janoChen Mar 8 '11 at 3:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.