2017-10-16 5 views
2

私は単一のmypost.phpを表示して、ユーザーがmsgsのフォームを送信できるmypostのシングルポストを表示できます(msgsはプライベートメッセージの別のカスタムポストタイプです) )。ユーザーがフォームを送信すると、新しい投稿はmsgsに完全に追加されますが、単一のmypostカスタムフィールドは空に変更されます。私はコードを使用して追加しました関数wp_insert_post()内の単一の{customposttype} .phpカスタムフィールドをクリアする

<form method="post" action="" class="gform" onSubmit="return validatebid()"> 
    <h2>Message</h2> 
    <input type="hidden" name="msgfor" value="<?php echo $post->ID; ?>"/> 
    <input type="hidden" name="msgby" value="<?php echo get_current_user_id(); ?>"/> 
    <input type="hidden" name="msgdate" value="<?php echo date('Y-m-d H:i:s'); ?>"/> 
    <div class="row"> 
     <label>Message</label> 
     <div class="field"> 
      <textarea name="textareafield"></textarea> 
     </div> 
    </div> 
<div class="row"> 
    <input type="hidden" name="task" value="msg" /> 
<input type="submit" name="submit" class="red-btn" value="Message Now"/> 
    </div> 
</form> 

一度ユーザーがwp_insert_postを使用してフォームを送信すると投稿を挿入します。コード私はget_headerの前に追加しました。私は、コードの2番目の部分は別のファイルにすべきだと思います

<?php if (isset($_POST[ 'textareafield' ])) {    
echo $_POST[ 'textareafield' ]; 
}?> 

+0

すべての必要なコード、特に挿入ポストに使用しているアクションを含めます。 –

+0

返信broのおかげで、これはフィールドと一度ユーザーが "wp_insert_post"を使って投稿したフォームです。これは私が追加した機能コードです。 –

答えて

0

それは次のようになりますが、追加してください:

require('wp-load.php'); // make sure that it is the correct path to this file 

... your code here ... 

オプションとして、AJAX経由でフォームを提出してください。wp_ajax_アクションhoo k。

重要なアイデアは、single-mypost.phpテンプレートの中にwp_insert_post()を使用していません。

0

<textarea name="textareafield"></textarea>

if (isset($_POST[ 'task' ]) && $_POST[ 'task' ] == 'msg') { 
$post_info = array(
    'post_title' => wp_strip_all_tags($_POST[ "msgfor" ] . '-' . 
    $_POST[ "msgby" ] . '-' . $_POST[ "msgdate" ]), 
    'post_content' => $_POST[ 'textareafield' ], 
    'post_type' => 'msgs', 
    'post_status' => 'publish' 
); 
$pid = wp_insert_post($post_info); 
echo $pid; 
update_post_meta($pid, "msgfor", $_POST[ "msgfor" ]); 
update_post_meta($pid, "msgby", $_POST[ "msgby" ]); 
update_post_meta($pid, "msgdate", $_POST[ "msgdate" ]); 
} 
関連する問題