当前位置: 首页 > wordpress分享 > 正文
wordpress添加非注册用户投稿页面
发布:2015-02-05 07:41:44 分类:wordpress分享 2条评论
为wordpress添加一个非注册用户投稿的功能,wordpress具有注册用户发布文章的功能,但是这个功能对于那些小wordpress用户来说非常不实用,因为很少有人为了投稿特意去注册一个账号来投稿!今天我们就做一个wordpress有个投稿页面。
一:创建wordpress游客投稿页面
- 在当前使用主题下新建一个page-tougao.php文件,然后将主题下page.php文件中的内容全部复制进去;
- 查找文件中引用正文内容的字符,例如<?php the_content(); ?>这段代码,然后修改为以下代码:
- <?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>
- <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">
- <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>
二:添加表单处理代码
- 复制以下代码到page-tougao.php的顶部。
- <?php
- /**
- * Template Name: tougao
- */
- 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>');
- }
- global $wpdb;
- $current_url = 'https://www.ziyouwu.com/?page_id=795'; // 注意修改此处的链接地址
- $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>');
- }
- }
- ?>
2.下载验证码验证文件,解压后放在当面主题的目录下;下载地址:http://yunpan.cn/cKEx4IU8xH5yy 访问密码 6fbe
3.进入后台,页面文件,模板处选择tougao,然后创建页面。把这个页面链接放到你首页的任何位置都可以。
这样一个简单的wordpress游客投稿页面就制作结束了。
本文参考了露兜博客的原文,代码和功能进行了部分修改和整合。比如直接整合了发表验证码功能。
有人制作完之后发现点此返回按钮直接跳转到了自由屋投稿页面,请你打开文件,把你的链接放在page-tougao.php文件中,文件中有备注。
最活跃的读者