【问题标题】:Publish a custom post type from front从前面发布自定义帖子类型
【发布时间】:2012-01-20 05:26:40
【问题描述】:

我想从前端发布自定义帖子类型“问题”,但是当我提交表单时,我不断收到 404 错误。波纹管是表格和表格处理。我做错了什么?

<?
/**
 * Questions processing
 */
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {

    // Do some minor form validation to make sure there is content
    if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
    } else {
        echo 'Please add a question';
    }
    if (isset ($_POST['description'])) {
        $description = $_POST['description'];
    } else {
        echo 'Please add a description';
    }

    // Add the content of the form to $post as an array
    $post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'post_status'   => 'publish',           
        'post_type' => 'question'                 
    );
    wp_insert_post($post);  // Pass  the value of $post to WordPress the insert function


} // end IF

// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post'); 

?>




<h1>Add a question:</h1>

<!-- New Question Form -->

<div>

<form name="new_post" method="post" action="">

<p><label for="title">Question:</label><br />

<input type="text" value="" name="title" />

</p>

<p><label for="description">Details</label><br />

<textarea name="description" cols="50" rows="6"></textarea>

</p>

<p><input type="submit" value="Ask!" name="submit" /></p>

<input type="hidden" name="post_type" value="question" />

<input type="hidden" name="action" value="new_post" />

<?php wp_nonce_field( 'new-post' ); ?>

</form>

</div>

<!--// New Question Form -->

【问题讨论】:

  • 也许可以尝试在 wp_insert_post($post) 之后拨打您的 do_action() 电话。
  • 您的验证步骤不会阻止脚本保存新帖子。那是你要的吗?如果您在 $_POST 中没有标题或描述,您将有两个未定义的变量($title 和 $description),您将尝试使用它们保存新帖子
  • 如果你替换 wp_insert_post($post); 你会看到什么$return = wp_insert_post($post); var_dump($return);
  • 我稍后会进行验证,感谢您的提示。现在我只想保存帖子。
  • 我看不到 var_dump,因为当我点击提交按钮时,我得到了 404 页面。

标签: php wordpress frontend custom-post-type


【解决方案1】:

您实际上并不需要 add_action 位,我感觉是 $post 变量本身导致了问题。

/**
 * Questions processing
 */
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {

    // Do some minor form validation to make sure there is content
    if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
    } else {
        echo 'Please add a question';
    }
    if (isset ($_POST['description'])) {
        $description = $_POST['description'];
    } else {
        echo 'Please add a description';
    }

    // Add the content of the form to $post as an array
    $new_post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'post_status'   => 'publish',           
        'post_type' => 'question'                 
    );
    $id = wp_insert_post($new_post);  // Pass  the value of $post to WordPress the insert function
    //Returns ID of the new post you just created

为了以防万一,还要在表单标签中添加一个 URL:

<form name="new_post" method="post" action="<?php the_permalink(); ?>">

【讨论】:

    【解决方案2】:

    我想通了:

    我已删除该行:

    <input type="hidden" name="post_type" value="question" />
    

    我认为 Wordpress 以某种方式使用了具有此名称的 post 变量,如果我自己使用它会出错。

    【讨论】:

      猜你喜欢
      • 2019-10-09
      • 2013-01-13
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2016-08-10
      • 1970-01-01
      相关资源
      最近更新 更多