带验证码完整的WordPress非注册用户投稿功能

开放投稿功能对于资讯型wordpress网站来说是丰富网站内容的有效手段,wordpress程序默认没有提供前台开放投稿功能,前面博客吧介绍《WordPress 博客不用插件实现投稿功能》可以实现读者前台投稿,但不具备验证码功能,很容易积累大量的垃圾投稿,增加网站管理人员的工作量,通过增加验证码功能,可以有效地减少垃圾稿件的投递!实现方法可以按下面的步骤制作!

一、添加投稿表单

  1. 首先在当前主题的目录下新建一个php文件,命名为tougao-page.php,然后将page.php中的所有代码复制到tougao-page.php中;
  2. 删除tougao-page.php开头的所有注释,即 /* 与 */ ,以及它们之间的所有内容;
  3. 搜索:the_content,可以查找到类似代码,将其替换成代码一

如果你在tougao-page.php中找不到the_content,那么你可以查找:get_template_part,可找到类似代码:,将content-page.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
<?php the_content(); ?>
<!-- 关于表单样式,请自行调整-->
<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>

二、添加表单处理代码

在tougao-page.php开头处中,将第一个 <?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
<?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)
);
// 将文章插入数据库
$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>');
}
}

三、给投稿页面添加验证码功能:

文件下载链接: http://pan.baidu.com/s/1qYFIfqW 密码: pm98

在代码一中的第35行,找到代码:

1
<br clear="all">

改为:

1
2
3
4
5
6
7
8
9
10
11
12
13
<div style="text-align: left; padding-top: 10px;">
<label for="CAPTCHA">验证码:
<input id="CAPTCHA" style="width:110px;*float:left;" class="input" type="text" tabindex="24" size="10" value="" name="captcha_code" /> 看不清?<a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='<?php bloginfo('template_url'); ?>/captcha/captcha.php?'+Math.random();document.getElementById('CAPTCHA').focus();return false;">点击更换</a>
</label>
</div>
 
<div style="text-align: left; padding-top: 10px;">
<label>
<img id="captcha_img" src="<?php bloginfo('template_url'); ?>/captcha/captcha.php" />
</label>
</div>
 
<br clear="all">

将代码二中的:

1
if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == 'send') {

改成:

1
2
3
4
5
6
7
8
9
10
11
12
if (!isset($_SESSION)) {
session_start();
session_regenerate_id(TRUE);
}
 
if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == 'send') {
if(empty($_POST['captcha_code'])
|| empty($_SESSION['ludou_lcr_secretword'])
|| (trim(strtolower($_POST['captcha_code'])) != $_SESSION['ludou_lcr_secretword'])
) {
wp_die('验证码不正确!<a href="'.$current_url.'">点此返回</a>');
}

WordPress自定义文章类型文章投稿教程:https://www.boke8.net/wordpress-post-type-contribution.html

代码来自露兜博客

原创文章,作者:kirin,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/247956.html

(0)
上一篇 2022年4月22日 06:02
下一篇 2022年4月22日 06:03

相关推荐

发表回复

登录后才能评论