2011-12-14 15 views
0

フロントエンドからカスタム投稿タイプ「質問」を公開したいが、フォームを送信すると404エラーが表示され続ける。ベローはフォームとフォームの処理です。私は間違って何をしていますか?フロントからカスタム投稿タイプを公開する

<? 
/** 
* Questions processing 
*/ 
if('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action'])) { 

    // Do some minor form validation to make sure there is content 
    if (isset ($_POST['title'])) { 
     $title = $_POST['title']; 
    } else { 
     echo 'Please add a question'; 
    } 
    if (isset ($_POST['description'])) { 
     $description = $_POST['description']; 
    } else { 
     echo 'Please add a description'; 
    } 

    // Add the content of the form to $post as an array 
    $post = array(
     'post_title' => $title, 
     'post_content' => $description, 
     'post_status' => 'publish',   
     'post_type' => 'question'     
    ); 
    wp_insert_post($post); // Pass the value of $post to WordPress the insert function 


} // end IF 

// Do the wp_insert_post action to insert it 
do_action('wp_insert_post', 'wp_insert_post'); 

?> 




<h1>Add a question:</h1> 

<!-- New Question Form --> 

<div> 

<form name="new_post" method="post" action=""> 

<p><label for="title">Question:</label><br /> 

<input type="text" value="" name="title" /> 

</p> 

<p><label for="description">Details</label><br /> 

<textarea name="description" cols="50" rows="6"></textarea> 

</p> 

<p><input type="submit" value="Ask!" name="submit" /></p> 

<input type="hidden" name="post_type" value="question" /> 

<input type="hidden" name="action" value="new_post" /> 

<?php wp_nonce_field('new-post'); ?> 

</form> 

</div> 

<!--// New Question Form --> 
+0

'do_action()'呼び出しは 'wp_insert_post($ post)'の直後に置いてください。 –

+0

あなたの検証ステップでは、スクリプトが新しい投稿を保存するのを止めません。それはあなたが欲しいものですか? $ _POSTにタイトルや説明がない場合は、2つの未定義の変数($ titleと$ description)があり、新しい投稿を保存しようとします。 – mishu

+0

wp_insert_post($ post)を置き換えるとどうなりますか? $ return = wp_insert_post($ post); var_dump($ return); – mishu

答えて

0

あなたが実際にadd_actionのビットを必要としないと私は感情が問題を引き起こしている$ポスト変数そのものであります。

/** 
* Questions processing 
*/ 
if('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action'])) { 

    // Do some minor form validation to make sure there is content 
    if (isset ($_POST['title'])) { 
     $title = $_POST['title']; 
    } else { 
     echo 'Please add a question'; 
    } 
    if (isset ($_POST['description'])) { 
     $description = $_POST['description']; 
    } else { 
     echo 'Please add a description'; 
    } 

    // Add the content of the form to $post as an array 
    $new_post = array(
     'post_title' => $title, 
     'post_content' => $description, 
     'post_status' => 'publish',   
     'post_type' => 'question'     
    ); 
    $id = wp_insert_post($new_post); // Pass the value of $post to WordPress the insert function 
    //Returns ID of the new post you just created 

そして、念のために、またあなたのformタグにURLを追加します。私は考え出し

<form name="new_post" method="post" action="<?php the_permalink(); ?>"> 
+0

私はすべて試しましたが、残念ながらそれは動作しません – zuzuleinen

0

<input type="hidden" name="post_type" value="question" /> 

:私は行を削除した

私はWordpressは何とかこの名前のポスト変数を使用していると私は自分自身でそれを使用するとエラーが発生すると思う。

関連する問題