2017-10-16 8 views
2

사용자가 메시지 양식을 제출할 수있는 mypost 단일 게시물을 표시하는 단일 -mypost.php가 있습니다 (msg는 비공개 메시지의 다른 사용자 정의 게시 유형 임)). 사용자가 양식을 제출하면 새 게시물이 msgs에 완벽하게 추가되지만 단일 mypost 사용자 정의 필드는 비어있는 것으로 변경됩니다. 나는 사용자가 양식에 게시물을 삽입 할 wp_insert_post을 사용하고 제출하면wp_insert_post() wp_insert_post() single- {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> 

을 사용하고 코드를 추가했습니다. 코드는 get_header 앞에 추가했습니다. 내가 코드의 두 번째 부분은 별도의 파일에 있어야한다고 생각

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

+0

모든 필수 코드, 특히 삽입 게시물에 사용하는 동작을 포함하십시오. –

+0

답장을 보내 주셔서 감사합니다. 필드가있는 양식과 사용자가 "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_ action hoo 케이.

핵심 아이디어는 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" ]); 
}