【问题标题】:Redirect to new submitted post重定向到新提交的帖子
【发布时间】:2018-03-22 08:05:59
【问题描述】:

我有一个表格,任何人都可以通过我的 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 代码的末尾,它会将用户重定向到他们创建的帖子...我尝试了不同的方法,但它不起作用。它不会重定向到任何地方。 你有什么想法?我怎样才能做到这一点?谢谢!

【问题讨论】:

    标签: php wordpress forms redirect


    【解决方案1】:

    你正在重复插入。这可能是一个问题。试试这个..它应该工作

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

    【讨论】:

    • 感谢拉尔夫的回答。我刚试过,整个网站现在都是空白的,什么都没有出现(html等)。它似乎与“exit();”有关,一旦我删除它,它就会再次起作用(但不是重定向)。你知道它为什么这样做吗?谢谢!
    • 你可以用 Die() 代替,但你使用什么并不重要。 die 是 exit 的别名:/
    • 如果这不起作用,我将需要更好的上下文,因为它可能会失败,具体取决于您尝试完成此操作的位置。你是从模板中尝试这个吗?插件?,函数文件?是什么触发了你的代码被执行。是否存在 post 元素?
    • 嗨拉尔夫!感谢您的帮助,我在这里找到了答案:stackoverflow.com/questions/46687913/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2017-10-05
    • 2013-09-27
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    相关资源
    最近更新 更多