使用WP仪表板上的表单创建帖子

时间:2015-11-11 作者:RiotAct

和往常一样,我有独特的情况和客户要求。我正在开发一个自定义仪表板小部件,它将包含一个用于注册事件的表单。提交表单后,它将生成一个自定义过帐类型的发票。

目前,我只是在玩一些基本的东西来让事情顺利进行。下面是我当前的代码:

function InvoiceRegisterContent() {

if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "add_new_post") {

    $new_post = array(
          \'ID\' => \'\',
          \'post_author\' => $user->ID, 
          \'post_title\' => $post_title,
          \'post_status\' => \'publish\'
        );

    $post_id = wp_insert_post($new_post);

    // This will redirect you to the newly created post
    $link = get_admin_url();
    wp_redirect( $link );

}

?>
    <form id="new_post" class="form-content" name="new_post" method="post" action="" />
        <input type="hidden" name="post_title" value="test" />
        <input type="hidden" name="post_type" value="post" />
        <input type="hidden" name="action" value="add_new_post" />
        <?php wp_nonce_field( \'add-post\' ); ?>
        <input type="submit" value="Register" class="button" name="submit" />   
    </form>
<?php 
}
?>
现在我的问题是,我可以看到一个“headers ready sent error”,我知道这是因为脚本开头的表单处理。是否有人知道如何正确地对表单处理进行编码,以便在发送标题之前完成?

我没有发布生成小部件的其余插件代码,因为这不是问题所在。

UPDATE

下面是我尝试使用admin\\u post的代码。这是在我正在构建的插件中。

function add_new_post() {
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "add_new_post") {

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

// Add the content of the form to $post as an array
$new_post = array(
        \'post_title\'    => $title,
        \'post_content\'  => $description,
        \'post_status\'   => \'publish\',           // Choose: publish, preview, future, draft, etc.
        \'post_type\' => \'post\'  //\'post\',page\' or use a custom post type if you want to
);
//save the new post
$pid = wp_insert_post($new_post);
wp_redirect(admin_url()); 
//insert taxonomies
    }
}

add_action ( \'admin_post_add_new_post\', \'InvoiceRegisterContent\');

function InvoiceRegisterContent() {

?>
    <form id="new_post" class="form-content" name="new_post" method="post" action="" />
        <input type="hidden" name="title" value="test" />
        <input type="hidden" name="action" value="add_new_post" />
        <?php wp_nonce_field( \'add-post\' ); ?>
        <input type="submit" value="Register" class="button" name="submit" />   
    </form>
<?php 
}
?>

ANOTHER UPDATE

使用当前代码(仍然不起作用,当我单击submit按钮时,它会打开一个新页面“admin post.php”,并再次显示不起作用的表单。

函数add\\u new\\u post(){if(\'post\'=$\\u SERVER[\'REQUEST\\u METHOD])&;!empty($\\u post[\'action])&;$\\u post[\'action]==“add\\u new\\u post”){

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

// Add the content of the form to $post as an array
$new_post = array(
        \'post_title\'    => $title,
        \'post_content\'  => $description,
        \'post_status\'   => \'publish\',           // Choose: publish, preview, future, draft, etc.
        \'post_type\' => \'post\'  //\'post\',page\' or use a custom post type if you want to
);
//save the new post
$pid = wp_insert_post($new_post);
wp_redirect(admin_url()); 
//insert taxonomies
    }
}

add_action ( \'admin_post_add_new_post\', \'InvoiceRegisterContent\');

function InvoiceRegisterContent() {

?>
    <form id="new_post" class="form-content" name="new_post" method="post" action="http://mps.ras01.com/wp-admin/admin-post.php" />
        <input type="hidden" name="title" value="test" />
        <input type="hidden" name="action" value="add_new_post" />
        <input type="submit" value="Register" class="button" name="submit" />   
    </form>
<?php 
}
?>

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

终于成功了!这只是一个让表单工作的基本测试,但如果其他人正在处理类似的问题,我就是这样让它工作的。另一件重要的事情是,要避免“Headers ready sent”错误,请确保没有错误,即所有字段都是完整的,因此不会返回错误消息。

function add_new_post() {
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "add_new_post") {

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

// Add the content of the form to $post as an array
$new_post = array(
        \'post_title\'    => $title,
        \'post_content\'  => $description,
        \'post_status\'   => \'publish\',           // Choose: publish, preview, future, draft, etc.
        \'post_type\' => \'post\'  //\'post\',page\' or use a custom post type if you want to
);
//save the new post
$pid = wp_insert_post($new_post);
wp_redirect(admin_url()); 
//insert taxonomies
    }
}

add_action ( \'init\', \'add_new_post\');

function InvoiceRegisterContent() {

?>
    <form id="new_post" class="form-content" name="new_post" method="post" action="" />
        <input type="hidden" name="title" value="test" />
        <input type="hidden" name="description" value="test" />
        <input type="hidden" name="action" value="add_new_post" />
        <input type="submit" value="Register" class="button" name="submit" />   
    </form>
<?php 
}
?>