2017-10-10 40 views
1

我有一個表單,任何人都可以通過我的wordpress網站上的鏈接訪問。 當用戶提交新帖子時,我希望他將其重定向到他們剛剛創建的帖子。重定向到新提交的文章

這裏是形式:

<?php 
wp_register_script('validation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js', array('jquery')); 
wp_enqueue_script('validation'); 


$post_information = array(
    'post_title' => wp_strip_all_tags($_POST['postTitle']), 
    'post_content' => $_POST['postContent'], 
    'post_type' => 'post', 
    'post_status' => 'publish' 
); 

wp_insert_post($post_information); 
$post_id = wp_insert_post($post_information); 
?> 


<form action="" id="primaryPostForm" method="POST" onsubmit="return getContent()"> 

<fieldset> 
    <label for="postTitle"><?php _e('Post Title:', 'framework') ?></label> 

    <input type="text" name="postTitle" id="postTitle" class="required" /> 
</fieldset> 

<fieldset> 
    <label for="postContent"><?php _e('Post Content:', 'framework') ?></label> 
    <textarea name="postContent" id="postContent" rows="8" cols="30" class="required"></textarea> 
</fieldset> 

<fieldset> 
    <input type="hidden" name="submitted" id="submitted" value="true" /> 

    <button type="submit"><?php _e('Add Post', 'framework') ?></button> 
    <?php wp_nonce_field('new-post'); ?> 
</fieldset> 

</form> 

我認爲,加入這一

wp_redirect(get_permalink($post_id)); 
die(); 

到PHP代碼的結束,將用戶重定向到他們所創建的帖子...我嘗試了不同的東西,但它不起作用。它不會在任何地方重定向。 你有什麼想法嗎?我怎樣才能做到這一點?謝謝!

回答

1

嗯,你是重複插入。這可能是一個問題。試試這個..它應該工作

$post_id = wp_insert_post($post_information); 
$url = get_permalink($post_id); 
wp_redirect($url); 
exit(); 
+1

謝謝你拉爾夫你的答案。我剛剛嘗試過,整個網站現在是空白的,什麼也沒有出現(HTML等)。它似乎與「退出();」,只要我刪除它再次工作(但不是重定向)。你知道它爲什麼這樣做嗎?謝謝! – heroickoala

+0

你可以用Die()來替換,但它不應該影響你的使用。死是一個別名退出:/ –

+0

如果這不起作用,我將需要更好的上下文,因爲它可能會失敗,取決於你試圖完成這一點。你是從模板來試試這個嗎?一個插件?,函數文件?什麼觸發你的代碼被執行。是否存在帖子元素? –