wordpress博客添加了自定义文章类型,然后想添加网站前台投稿功能,把投稿的文章提交到指定的wordpress自定义文章类型怎么办?方法其实很简单.
完整代码如下:
<?php
if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == 'send') {
global $wpdb;
$current_url = 'http://你的投稿页面地址'; // 注意修改此处的链接地址
$last_post = $wpdb->get_var("SELECT `post_date` FROM `$wpdb->posts` ORDER BY `post_date` DESC LIMIT 1");
// 博客当前最新文章发布时间与要投稿的文章至少间隔120秒。
// 可自行修改时间间隔,修改下面代码中的120即可
// 相比Cookie来验证两次投稿的时间差,读数据库的方式更加安全
if ( current_time('timestamp') - strtotime($last_post) < 120 ) {
wp_die('您投稿也太勤快了吧,先歇会儿!<a href="'.$current_url.'">点此返回</a>');
}
// 表单变量初始化
$name = isset( $_POST['tougao_authorname'] ) ? trim(htmlspecialchars($_POST['tougao_authorname'], ENT_QUOTES)) : '';
$email = isset( $_POST['tougao_authoremail'] ) ? trim(htmlspecialchars($_POST['tougao_authoremail'], ENT_QUOTES)) : '';
$blog = isset( $_POST['tougao_authorblog'] ) ? trim(htmlspecialchars($_POST['tougao_authorblog'], ENT_QUOTES)) : '';
$title = isset( $_POST['tougao_title'] ) ? trim(htmlspecialchars($_POST['tougao_title'], ENT_QUOTES)) : '';
$category = isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0;
$content = isset( $_POST['tougao_content'] ) ? trim(htmlspecialchars($_POST['tougao_content'], ENT_QUOTES)) : '';
// 表单项数据验证
if ( empty($name) || mb_strlen($name) > 20 ) {
wp_die('昵称必须填写,且长度不得超过20字。<a href="'.$current_url.'">点此返回</a>');
}
if ( empty($email) || strlen($email) > 60 || !preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)) {
wp_die('Email必须填写,且长度不得超过60字,必须符合Email格式。<a href="'.$current_url.'">点此返回</a>');
}
if ( empty($title) || mb_strlen($title) > 100 ) {
wp_die('标题必须填写,且长度不得超过100字。<a href="'.$current_url.'">点此返回</a>');
}
if ( empty($content) || mb_strlen($content) > 3000 || mb_strlen($content) < 100) {
wp_die('内容必须填写,且长度不得超过3000字,不得少于100字。<a href="'.$current_url.'">点此返回</a>');
}
$post_content = '昵称: '.$name.'<br />Email: '.$email.'<br />blog: '.$blog.'<br />内容:<br />'.$content;
$tougao = array(
'post_title' => $title,
'post_content' => $post_content,
'post_category' => array($category),
'post_type' => 'tougao_type' //tougao_type是要保存到的自定义文章类型
);
// 将文章插入数据库
$status = wp_insert_post( $tougao );
if ($status != 0) {
// 投稿成功给博主发送邮件
// somebody#example.com替换博主邮箱
// My subject替换为邮件标题,content替换为邮件内容
wp_mail("somebody#example.com","My subject","content");
wp_die('投稿成功!感谢投稿!<a href="'.$current_url.'">点此返回</a>', '投稿成功');
}
else {
wp_die('投稿失败!<a href="'.$current_url.'">点此返回</a>');
}
}
?>
<form class="ludou-tougao" method="post" action="<?php echo $_SERVER["REQUEST_URI"]; $current_user = wp_get_current_user(); ?>">
<div style="text-align: left; padding-top: 10px;">
<label for="tougao_authorname">昵称:*</label>
<input type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_login; ?>" id="tougao_authorname" name="tougao_authorname" />
</div>
<div style="text-align: left; padding-top: 10px;">
<label for="tougao_authoremail">E-Mail:*</label>
<input type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_email; ?>" id="tougao_authoremail" name="tougao_authoremail" />
</div>
<div style="text-align: left; padding-top: 10px;">
<label for="tougao_authorblog">您的博客:</label>
<input type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_url; ?>" id="tougao_authorblog" name="tougao_authorblog" />
</div>
<div style="text-align: left; padding-top: 10px;">
<label for="tougao_title">文章标题:*</label>
<input type="text" size="40" value="" id="tougao_title" name="tougao_title" />
</div>
<div style="text-align: left; padding-top: 10px;">
<label for="tougaocategorg">分类:*</label>
<?php wp_dropdown_categories('hide_empty=0&id=tougaocategorg&show_count=1&hierarchical=1'); ?>
</div>
<div style="text-align: left; padding-top: 10px;">
<label style="vertical-align:top" for="tougao_content">文章内容:*</label>
<textarea rows="15" cols="55" id="tougao_content" name="tougao_content"></textarea>
</div>
<br clear="all">
<div style="text-align: center; padding-top: 10px;">
<input type="hidden" value="send" name="tougao_form" />
<input type="submit" value="提交" />
<input type="reset" value="重填" />
</div>
</form>
代码说明:
1、上面代码'post_type' => 'tougao_type'
中的tougao_type
就是设置要保存到的自定义文章类型参数,改为自己网站的自定义文章类型即可。
2、代码中的$tougao数组变量只设置了几项参数,完整的参数如下:
$tougao = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => 1,
'post_content' => $post_content,
'post_name' => sanitize_title($title),
'post_status' => 'private',
'post_title' => $title,
'tags_input' => $tags,
'post_category' => $category_list,
'post_date' => $postdate,
'post_type' => 'tougao_type',
'tax_input' => array(
'tougao_category' => array(
$getcategory
)
)
);
参数说明:
comment_status
– 评论状态,closed表示关闭评论,默认开启ping_status
– ping状态,closed表示关闭post_author
– 作者idpost_content
– 正文内容,变量$post_content表示获取的内容post_name
– 文章别名post_status
– 文章状态,pending(待审)、draft(草稿)、auto-draft(自动保存的草稿)、inherit(修订版本)、trash(回收站)、publish(已发布)、future(定时)、private(私有)post_title
– 文章标题,变量$title表示获取的标题内容tags_input
– 文章标签,字符串,变量$tags是获取到的标签post_category
– 文章分类,值为数组,变量$category_list是获取到的分类post_date
– 文章时间,变量$postdate是获取到的时间post_type
– 文章类型,默认是post,可选page或自定义文章类型tax_input
– 自定义分类法,参数tougao_category是自定义分类法名称,变量$getcategory是获取到的分类
根据说明调整代码参数值。
原文链接:https://www.fogwhale.cn/?p=1143,转载请注明出处。
评论0