在Wordpress上提交表单后重定向到Homeurl

时间:2019-06-30 07:18:23

标签: php wordpress

我设法制作了一个前端帖子提交表单,供用户创建帖子。 因此,单击“提交”后,它会停留在同一页面上。 帖子提交后,我需要重定向到首页网址。 这是我的代码。

  <main class="main-content">
    <div class="add-question">
    <div class="phdr">Add Post</div>
    <div class="question-form">
    <form method="post" enctype="multipart/form-data" name="mainForm" action="">
      <div id="singleinput">
      <label></label>
      <input type="text" name="postTitle" class="postTitle" placeholder="What’s your Post title?."/>
      </div>

      <div id="singleinput">
      <label>Description: </label>
      <textarea rows="4" cols="20" class="postContent" name="postContent" placeholder="Tell us more about your Post"></textarea>
      </div>
      <div id="singleinput">
  <label for="cat">Type:</label>
  <?php wp_dropdown_categories(       'tab_index=10&taxonomy=category&hide_empty=0' ); ?>
      </div>

<div id="singleinput">
<label>Tags</label>
<input type="text" name="postTags" class="postTags" placeholder="e.g. (abcd,efgh)"/>
</div>

      <div id="singleinput">
        <input type="submit" name="add_post" id="add_post" value="Post">
      </div>
    </form>
  <?php

  if($_POST['add_post']){

  wp_insert_post(array(
  'post_type' => 'post',
  'post_title' => $_POST['postTitle'],
  'post_content' => $_POST['postContent'],
  'post_status' => 'publish',
  'post_category'   =>  array($_POST['cat']),
  'tags_input'  =>  $_POST['postTags'],
  ));
  }
  ?>
  </div>
  </div>
  </main>
提前

tnx

编辑::

<?php

if($_POST['add_post']){
$post_information = array(
    'post_type' => 'post',
    'post_title' => $_POST['postTitle'],
    'post_content' => $_POST['postContent'],
    'post_status' => 'publish',
    'post_category' =>  array($_POST['cat']),
    'tags_input'    =>  $_POST['postTags'],
    );
    $post_id = wp_insert_post($post_information);
    global $wpdb;
    $redirect_link = get_home_url();
    wp_redirect($redirect_link);
}
get_header(); ?>

此代码完成了工作。 谢谢大家!

3 个答案:

答案 0 :(得分:0)

if($_POST['add_post']){
    $post_data = array(
    'post_type' => 'post',
    'post_title' => $_POST['postTitle'],
    'post_content' => $_POST['postContent'],
    'post_status' => 'publish',
    'post_category'   =>  array($_POST['cat']),
    'tags_input'  =>  $_POST['postTags'],
  );
  $post_id = wp_insert_post( $post_data );
  if($post_id){
    $homrurl = get_home_url();
    header('Location: '$homrurl);
  }
  }

答案 1 :(得分:0)

您可以在 wp_insert_post 函数wp_redirect(home_url());

之后添加此行

答案 2 :(得分:0)

您需要确保没有错误。如果插入成功,则可以使用header("Location:"),否则,您应该通知发生了错误:

if(isset($_POST['add_post'])){
    $result = wp_insert_post(array(
        'post_type' => 'post',
        'post_title' => $_POST['postTitle'],
        'post_content' => $_POST['postContent'],
        'post_status' => 'publish',
        'post_category'   =>  array($_POST['cat']),
        'tags_input'  =>  $_POST['postTags'],
    ));
    if($result > 0 && !is_wp_error($result)){
        header("Location:" . get_home_url());
        exit();
    } else {
        echo '<script>alert("The post could not be saved")</script>';
    }
}