使用元密钥的前端提交

时间:2012-07-20 作者:IFightCode

我正在创建一个前端自定义帖子类型提交源。我已经对不同的芭蕾舞进行了编码检查。但是表单在suvmission时返回not found页面,再次没有输入任何数据。如果可能的话,请帮我找出错误。

<?php
if (!isset($_POST[\'submit\'])) {

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

    <table>

        <tr><th width="138"><label for="id_meta">Mobile No:</label></th><td width="163"><input id="id_meta" type="text" name="wpcf-mob_no" maxlength="100" /></td></tr>

        <tr><th width="138"><label for="id_meta">Amount To Load:</label></th><td width="163"><input id="id_meta" type="text" name="wpcf-mob_amount" maxlength="100" /></td></tr>

        <tr><th width="138"><label for="id_meta">Connection Type</label></th><td width="163"><select id="id_meta" type="text" name="wpcf-mob_type" maxlength="100" />
          <option value="pre_paid">Pre Paid</option>
          <option value="post_paid">Post Paid</option>
        </td></tr>

        <tr><th width="138"></th><td width="163"><input id="id_meta" type="hidden" name="wpcf-mob_status" maxlength="100" value="pending"/></td></tr>

        <tr><th></th><td><input id="id_title" type="hidden" name="post_title" maxlength="100" value="<?php echo date(\'r\'); ?>"/></td></tr>


        <tr>

            <td>&nbsp;</td>
<input type="hidden" name="post_type" id="post_type" value="loads" />

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

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

            <td><input type="submit" value="Submit" name="submit"/></td>

        </tr>

    </table>



</form>

<?php }

else {

        $title = $_POST[\'post_title\'];
        $meta_box1 = $_POST[\'wpcf-mob_no\'];
        $meta_box2 = $_POST[\'wpcf-mob_amount\'];
        $meta_box3 = $_POST[\'wpcf-mob_type\'];
        $meta_box4 = $_POST[\'wpcf-mob_status\'];

        $new_post = array(
            \'post_title\'    => $title,
            \'post_status\'   => \'publish\',           // Choose: publish, preview, future, draft, etc.
            \'post_type\' => \'loads\'  //\'post\',page\' or use a custom post type if you want to
    );
    //save the new post
    $pid = wp_insert_post($new_post);

    /* Insert Form data into Custom Fields */
    add_post_meta($pid, \'wpcf-mob_no\', $meta_box1, true);
    add_post_meta($pid, \'wpcf-mob_amount\', $meta_box2, true);
    add_post_meta($pid, \'wpcf-mob_type\', $meta_box3, true);
    add_post_meta($pid, \'wpcf-mob_status\', $meta_box4, true);


    print \'<pre>\';
    var_dump(array($new_post, $pid));
    print \'</pre>\';
}

?>

2 个回复
最合适的回答,由SO网友:Sagive 整理而成

由于元数据字段已经存在,您应该更新它
您还应该计算wp\\U insert\\U post的时间。。。这是修改后的代码

尝试一下,如果遇到问题,请告诉我:

else {

        $title = $_POST[\'post_title\'];
        $meta_box1 = $_POST[\'wpcf-mob_no\'];
        $meta_box2 = $_POST[\'wpcf-mob_amount\'];
        $meta_box3 = $_POST[\'wpcf-mob_type\'];
        $meta_box4 = $_POST[\'wpcf-mob_status\'];

        $new_post = array(
            \'post_title\'    => $title,
            \'post_status\'   => \'publish\',           // Choose: publish, preview, future, draft, etc.
            \'post_type\' => \'loads\'  //\'post\',page\' or use a custom post type if you want to
    );
    //save the new post
    $pid    =   wp_insert_post($new_post, 10, 1);

    // Do the wp_insert_post action to insert it
    do_action(\'wp_insert_post\', \'wp_insert_post\', 10, 1); 
    update_metadata($pid, \'wpcf-mob_no\', $meta_box1, true);
    update_metadata($pid, \'wpcf-mob_amount\', $meta_box2, true);
    update_metadata($pid, \'wpcf-mob_type\', $meta_box3, true);
    update_metadata($pid, \'wpcf-mob_status\', $meta_box4, true);


    print \'<pre>\';
    var_dump(array($new_post, $pid));
    print \'</pre>\';
}

SO网友:PHPLearner

下面的代码对我有用。

add_post_meta($pid, \'wpcf-mob_no\', $meta_box1, true);
add_post_meta($pid, \'wpcf-mob_amount\', $meta_box2, true);
add_post_meta($pid, \'wpcf-mob_type\', $meta_box3, true);
add_post_meta($pid, \'wpcf-mob_status\', $meta_box4, true);

结束

相关推荐