WordPress 添加投稿功能

WordPress网站开放投稿功能,接受读者的投稿。但WordPress本身并不提供投稿功能,只拥有强大的扩展能力,我们可以自己添加这个投稿功能。

实现用户投稿,有两种方法:

一种是开放后台注册功能,普通用户注册进去默认设置为投稿者,登陆进去即可添加文章(默认为草稿);

另一种是在前台提供投稿表单,用户填写相应的表格,例如米扑博客:http://blog.mimvp.com

WordPress 添加投稿功能_第1张图片

前一种方法实现起来比较简单,基本不需要配置太多,只是有些博主可能会觉得别扭,不愿让他人看到自己的博客后台;而后一种方法对投稿者来说方便了很多,博主也不用担心自己博客的后台隐私,只是该方法实现起来比较麻烦,需要配置的东西很多。本文只将介绍后一种方法,希望对你有所帮助,当然也只是复制粘贴代码就可以了。

一、添加投稿表单

1、首先在当前主题目录(/wp-content/themes/your_theme/pages/)下新建一个php文件,命名为tougao.php,并将page.php中的所有代码复制到tougao.php中(tougao.php名称可能在your_theme/pages/tougao.php下,也可能在your_theme/template-tougao.php);

tougao.php 代码

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
  * template name: 投稿
  * website demo : http://blog.mimvp.com
  * description: template for mimvp.com
  * author : yanggang
  * date   : 2015-11-20
  */
 
if ( isset( $_POST [ 'tougao_form' ]) && $_POST [ 'tougao_form' ] == 'mimvp_auto_post' ){
     if ( isset( $_COOKIE [ "tougao" ]) && ( time() - $_COOKIE [ "tougao" ] ) < 120 ){
         wp_die( '您投稿也太勤快了吧,先歇会儿!' );
     }
     print_r( $_POST );
     //表单变量初始化
     $authorname = isset( $_POST [ 'tougao_authorname' ] ) ? $_POST [ 'tougao_authorname' ] : '' ;
     $email = isset( $_POST [ 'tougao_authoremail' ] ) ? $_POST [ 'tougao_authoremail' ] : '' ;
     $blog = isset( $_POST [ 'tougao_authorblog' ] ) ? $_POST [ 'tougao_authorblog' ] : '' ;
     $title = isset( $_POST [ 'tougao_title' ] ) ? $_POST [ 'tougao_title' ] : '' ;
     $tags = isset( $_POST [ 'tougao_tags' ] ) ? $_POST [ 'tougao_tags' ] : '' ;
     $category = isset( $_POST [ 'tougao_cat' ] ) ? $_POST [ 'tougao_cat' ] : '4766' ;
     $postdate = isset( $_POST [ 'tougao_date' ] ) ? $_POST [ 'tougao_date' ] : date ( "Y-m-d H:i:s" , strtotime ( "+8 hour" ));
     $content = isset( $_POST [ 'tougao_content' ] ) ? $_POST [ 'tougao_content' ] : '' ;
 
     $category_list = explode ( "," , $category );
     //    $category_list = array("4924","1798","783","771");
 
/*
     echo $postdate;
     //表单项数据验证
     if ( empty($authorname) || strlen($authorname) > 20 ){
         wp_die('昵称必须填写,且不得超过20个长度');
     }
     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('邮箱必须填写,且不得超过60个长度,必须符合email格式');
     }
     if ( empty($title) || strlen($title) > 100 ){
         wp_die('文章标题必须填写,且不得超过100个长度');
     }
     if ( empty($content) || strlen($content) < 100){
         wp_die('内容必须填写,且不得少于100个长度');
     }
  */
 
     $tougao = array ( 'post_title' => $title ,
         'post_content' => $content ,
         'post_status' => 'publish' ,
         'tags_input' => $tags ,
         'post_date' => $postdate ,
         // 'post_category' => array($category));
         'post_category' => $category_list );
 
     // 将文章插入数据库
     $status = wp_insert_post( $tougao );               
     if ( $status != 0){
         global $wpdb ;
         $myposts = $wpdb ->get_results( "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC" );
         add_post_meta( $myposts [0]->ID, 'tcp_postauthor' , $authorname );  //插入投稿人昵称的自定义域
         if ( ! empty ( $blog ))
             add_post_meta( $myposts [0]->ID, 'tcp_posturl' , $blog );       //插入投稿人网址的自定义域
         setcookie( "tougao" , time(), time()+180);
         wp_die( '投稿成功!' , '投稿成功!' );
     } else {
         wp_die( '投稿失败!' , '投稿失败!' );
     }
}
 
get_header();
?>
 
 
if (have_posts()) : ?>
while (have_posts()) : the_post(); ?>
include ( 'top.php' ); ?>
"wrapper" class = "clearfix" >
"navigation" >
class = "navigation" >
include ( "includes/notice.php" ); ?>
class = "tougao divmargin" >
 
class = "entry" >
 
"post" action= " REQUEST_URI "]; $current_user = wp_get_current_user(); ?>" >
"basicinfo" >
 
"width: 700px; line-height: 30px; font-size: 16px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
"width: 75px; text-align: right; padding-right: 10px;" >昵称:
"text" style= "width: 200px;" name= "tougao_authorname" value= "ID ) echo $current_user->user_login; ?>" />
*
"width: 75px; text-align: right; padding-right: 10px;" >E-Mail:
"text" style= "width: 200px;" name= "tougao_authoremail" value= "ID ) echo $current_user->user_email; ?>" />
*
"width: 75px; text-align: right; padding-right: 10px;" >您的网站:
"text" style= "width: 200px;" name= "tougao_authorblog" value= "ID ) echo $current_user->user_url; ?>" />
"width: 75px; text-align: right; padding-right: 10px;" >文章标题:
"text" style= "width: 200px;" name= "tougao_title" value= "" />
*
"width: 75px; text-align: right; padding-right: 10px;" >关键字:
"text" style= "width: 200px;" value= "" name= "tougao_tags" />
*
"width: 75px; text-align: right; padding-right: 10px;" >文章分类:
//wp_dropdown_categories('show_count=1&hierarchical=1'); ?>
'title_li=0&hierarchical=1' ); ?>
*
"2" >
文章内容:(必须)
"2" >
class = "post-area" >
发布密码:
"password" style= "width: 200px;" value= "123456" name= "tougao_form" />
*
"2" >
"submit" name= "submit" type= "submit" value= "提交" />
   
"reset" name= "submit" type= "reset" value= "重填" />

你可能感兴趣的:(NetWork)