2017-10-10 41 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

如果这不起作用,我将需要更好的上下文,因为它可能会失败,取决于你试图完成这一点。你是从模板来试试这个吗?一个插件?,函数文件?什么触发你的代码被执行。是否存在帖子元素? –