wordpress前台投稿支持自定义文章类型文章

WordPress教程 2736

wordpress博客添加了自定义文章类型,然后想添加网站前台投稿功能,把投稿的文章提交到指定的wordpress自定义文章类型怎么办?方法其实很简单,博客吧以前分享过wordpress实现前台投稿功能教程,只需要把该教程的代码参数进行一下修改即可。

完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?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数组变量只设置了几项参数,完整的参数如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$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 – 作者id
  • post_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是获取到的分类

根据说明调整代码参数值。

精品推荐: