您可以使用插件进行前端发布:
或者您可以自己创建表单
<!-- 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_post\')) {
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\';
}
$tags = $_POST[\'post_tags\'];
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'post_category\' => $_POST[\'cat\'],
\'tags_input\' => $tags,
\'post_status\' => \'draft\'
);
wp_insert_post($new_post);
}
并确保检查表单字段的nonce和清理。
根据您的要点代码和注释进行更新将您的功能更改为:
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && ($_POST[\'action\']== \'new_post\')) {
$has_errors = false;
if (isset ($_POST[\'title\'])) {
$title = $_POST[\'title\'];
} else {
echo \'Please enter a title\';
$has_errors = true;
}
if (isset ($_POST[\'performer\'])) {
$performer = $_POST[\'preformer\'];
} else {
echo \'Please enter a performer\';
$has_errors = true;
}
if (isset ($_POST[\'composer\'])) {
$composer = $_POST[\'composer\'];
} else {
echo \'Please enter a composer\';
$has_errors = true;
}
if (isset ($_POST[\'tablature\'])) {
$tablature = $_POST[\'tablature\'];
} else {
echo \'Please enter the content\';
$has_errors = true;
}
$tags = $_POST[\'post_tags\'];
if (!$has_errors){
//save <title> by: <preformer>
$title .= " by: " .$performer;
//save Composed by: <composer> Performed by: <performer> <tablature>
$content = "<h4>Composed by: ". $composer."</h4><br/><h4>Performed by: ".$performer."</h4><br/>".$tablature;
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $content,
\'post_category\' => $_POST[\'cat\'],
\'tags_input\' => $tags,
\'post_status\' => \'draft\'
);
$pid = wp_insert_post($new_post);
//save email and submmiter as post meta in custom fields
update_post_meta($pid, \'submiter_email\', urldecode($_POST[\'email\']));
update_post_meta($pid, \'submiter_name\', urldecode($_POST[\'submitter\']));
}
}