要从前端发布,可以使用wp_insert_post() 作用
所以这只是一个表单和处理它的问题
表格:
<!-- New Post Form -->
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="">
<p><label for="title">Title</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title" />
</p>
<p><label for="description">Description</label><br />
<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
</p>
<p><?php wp_dropdown_categories( \'show_option_none=Category&tab_index=4&taxonomy=category\' ); ?></p>
<p><label for="post_tags">Tags</label>
<input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( \'new-post\' ); ?>
</form>
</div>
<!--// New Post Form -->
处理过程:
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == "new_game_post") {
// Do some minor form validation to make sure there is content
if (isset ($_POST[\'title\'])) {
$title = $_POST[\'title\'];
} else {
echo \'Please enter a game title\';
}
if (isset ($_POST[\'description\'])) {
$description = $_POST[\'description\'];
} else {
echo \'Please enter the content\';
}
$tags = $_POST[\'post_tags\'];
// Add the content of the form to $post as an array
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'tags_input\' => array($tags),
\'post_status\' => \'publish\', // Choose: publish, preview, future, draft, etc.
\'post_type\' => $_POST[\'post_type\'] // Use a custom post type if you want to
);
//save the new post and return its ID
$pid = wp_insert_post($new_post);
}
Bonus 一旦有了post id(上例中为pid),就可以轻松设置术语和自定义字段:
//insert taxonomies like categories tags or custom
wp_set_post_terms($pid,(array)($_POST[\'cat\']),\'category\',true);
//insert custom fields
update_post_meta($pid,\'meta_key_name\',$_POST[\'meta_value\']);
希望这有帮助。