自由屋推书网—热门的小说推荐平台!

你的位置: 首页 > wordpress分享

wordpress添加非注册用户投稿页面

2015-02-05 07:41:44

为wordpress添加一个非注册用户投稿的功能,wordpress具有注册用户发布文章的功能,但是这个功能对于那些小wordpress用户来说非常不实用,因为很少有人为了投稿特意去注册一个账号来投稿!今天我们就做一个wordpress有个投稿页面。

一:创建wordpress游客投稿页面

  1. 在当前使用主题下新建一个page-tougao.php文件,然后将主题下page.php文件中的内容全部复制进去;
  2. 查找文件中引用正文内容的字符,例如<?php the_content(); ?>这段代码,然后修改为以下代码:
  1. <?php the_content(); ?>
  2. <!-- 关于表单样式,请自行调整-->
  3. <form class="ludou-tougao" method="post" action="<?php echo $_SERVER["REQUEST_URI"]; $current_user = wp_get_current_user(); ?>">
  4.     <div style="text-align: left; padding-top: 10px;">
  5.         <label for="tougao_authorname">昵称:*</label>
  6.         <input type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_login; ?>" id="tougao_authorname" name="tougao_authorname" />
  7.     </div>
  8.     <div style="text-align: left; padding-top: 10px;">
  9.         <label for="tougao_authoremail">E-Mail:*</label>
  10.         <input type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_email; ?>" id="tougao_authoremail" name="tougao_authoremail" />
  11.     </div>
  12.     <div style="text-align: left; padding-top: 10px;">
  13.         <label for="tougao_authorblog">您的博客:</label>
  14.         <input type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_url; ?>" id="tougao_authorblog" name="tougao_authorblog" />
  15.     </div>
  16.     <div style="text-align: left; padding-top: 10px;">
  17.         <label for="tougao_title">文章标题:*</label>
  18.         <input type="text" size="40" value="" id="tougao_title" name="tougao_title" />
  19.     </div>
  20.     <div style="text-align: left; padding-top: 10px;">
  21.         <label for="tougaocategorg">分类:*</label>
  22.         <?php wp_dropdown_categories('hide_empty=0&id=tougaocategorg&show_count=1&hierarchical=1'); ?>
  23.     </div>
  24.     <div style="text-align: left; padding-top: 10px;">
  25.         <label style="vertical-align:top" for="tougao_content">文章内容:*</label>
  26.         <textarea rows="15" cols="55" id="tougao_content" name="tougao_content"></textarea>
  27.     </div>
  28. <div style="text-align: left; padding-top: 10px;">
  29.   <label for="CAPTCHA">验证码:
  30.     <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>
  31.   </label>
  32. </div>
  33. <div style="text-align: left; padding-top: 10px;">
  34.   <label>
  35.     <img id="captcha_img" src="<?php bloginfo('template_url'); ?>/captcha/captcha.php" />
  36.   </label>
  37. </div>
  38. <br clear="all">
  39.     <div style="text-align: center; padding-top: 10px;">
  40.         <input type="hidden" value="send" name="tougao_form" />
  41.         <input type="submit" value="提交" />
  42.         <input type="reset" value="重填" />
  43.     </div>
  44. </form>

二:添加表单处理代码

  1. 复制以下代码到page-tougao.php的顶部。
  1. <?php
  2. /**
  3.  * Template Name: tougao
  4.  */
  5. if (!isset($_SESSION)) {
  6.     session_start();
  7.     session_regenerate_id(TRUE);
  8. }
  9. if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == 'send') {
  10.   if(empty($_POST['captcha_code'])
  11.     || empty($_SESSION['ludou_lcr_secretword'])
  12.     || (trim(strtolower($_POST['captcha_code'])) != $_SESSION['ludou_lcr_secretword'])
  13.   ) {
  14.     wp_die('验证码不正确!<a href="'.$current_url.'">点此返回</a>');
  15.   }
  16.     global $wpdb;
  17.     $current_url = 'https://www.ziyouwu.com/?page_id=795';   // 注意修改此处的链接地址
  18.     $last_post = $wpdb->get_var("SELECT `post_date` FROM `$wpdb->posts` ORDER BY `post_date` DESC LIMIT 1");
  19.     // 博客当前最新文章发布时间与要投稿的文章至少间隔120秒。
  20.     // 可自行修改时间间隔,修改下面代码中的120即可
  21.     // 相比Cookie来验证两次投稿的时间差,读数据库的方式更加安全
  22.     if ( (current_time('timestamp') - strtotime($last_post)) < 120 ) {
  23.         wp_die('您投稿也太勤快了吧,先歇会儿!<a href="'.$current_url.'">点此返回</a>');
  24.     }
  25.     // 表单变量初始化
  26.     $name = isset( $_POST['tougao_authorname'] ) ? trim(htmlspecialchars($_POST['tougao_authorname'], ENT_QUOTES)) : '';
  27.     $email =  isset( $_POST['tougao_authoremail'] ) ? trim(htmlspecialchars($_POST['tougao_authoremail'], ENT_QUOTES)) : '';
  28.     $blog =  isset( $_POST['tougao_authorblog'] ) ? trim(htmlspecialchars($_POST['tougao_authorblog'], ENT_QUOTES)) : '';
  29.     $title =  isset( $_POST['tougao_title'] ) ? trim(htmlspecialchars($_POST['tougao_title'], ENT_QUOTES)) : '';
  30.     $category =  isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0;
  31.     $content =  isset( $_POST['tougao_content'] ) ? trim(htmlspecialchars($_POST['tougao_content'], ENT_QUOTES)) : '';
  32.     // 表单项数据验证
  33.     if ( empty($name) || mb_strlen($name) > 20 ) {
  34.         wp_die('昵称必须填写,且长度不得超过20字。<a href="'.$current_url.'">点此返回</a>');
  35.     }
  36.     if ( empty($email) || strlen($email) > 60 || !preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)) {
  37.         wp_die('Email必须填写,且长度不得超过60字,必须符合Email格式。<a href="'.$current_url.'">点此返回</a>');
  38.     }
  39.     if ( empty($title) || mb_strlen($title) > 100 ) {
  40.         wp_die('标题必须填写,且长度不得超过100字。<a href="'.$current_url.'">点此返回</a>');
  41.     }
  42.     if ( empty($content) || mb_strlen($content) > 3000 || mb_strlen($content) < 100) {
  43.         wp_die('内容必须填写,且长度不得超过3000字,不得少于100字。<a href="'.$current_url.'">点此返回</a>');
  44.     }
  45.     $post_content = '昵称: '.$name.'<br />Email: '.$email.'<br />blog: '.$blog.'<br />内容:<br />'.$content;
  46.     $tougao = array(
  47.         'post_title' => $title,
  48.         'post_content' => $post_content,
  49.         'post_category' => array($category)
  50.     );
  51.     // 将文章插入数据库
  52.     $status = wp_insert_post( $tougao );
  53.     if ($status != 0) {
  54.         // 投稿成功给博主发送邮件
  55.         // somebody#example.com替换博主邮箱
  56.         // My subject替换为邮件标题,content替换为邮件内容
  57.         wp_mail("somebody#example.com","My subject","content");
  58.         wp_die('投稿成功!感谢投稿!<a href="'.$current_url.'">点此返回</a>', '投稿成功');
  59.     }
  60.     else {
  61.         wp_die('投稿失败!<a href="'.$current_url.'">点此返回</a>');
  62.     }
  63. }
  64. ?>

2.下载验证码验证文件,解压后放在当面主题的目录下;下载地址:http://yunpan.cn/cKEx4IU8xH5yy  访问密码 6fbe

3.进入后台,页面文件,模板处选择tougao,然后创建页面。把这个页面链接放到你首页的任何位置都可以。

这样一个简单的wordpress游客投稿页面就制作结束了。

wordpress游客发表

本文参考了露兜博客的原文,代码和功能进行了部分修改和整合。比如直接整合了发表验证码功能。

有人制作完之后发现点此返回按钮直接跳转到了自由屋投稿页面,请你打开文件,把你的链接放在page-tougao.php文件中,文件中有备注。

编辑推荐

热门小说