所有分类
  • 所有分类
  • WordPress主题
  • WordPress插件
  • 源码
  • 软件
  • 主题教程

WordPress添加投稿功能的方法

很多网站都想开放读者的投稿功能,但WordPress本身并不提供投稿功能,我们可以自己来添加。

实现用户投稿,有两种方法,一种是开放后台的注册功能,普通用户注册进去默认设置为投稿者,登陆进去即可添加文章(默认为草稿);另一种方法是在前台提供投稿表单,用户填写相应的表格即可。后一种方法对投稿者来说方便了很多,博主也不用担心自己博客的后台隐私,只是该方法实现起来比较麻烦,需要配置的东西很多。本文也只将介绍后一种方法。

一、添加投稿表单

1、首先在当前主题的目录下新建一个php文件,命名为tougao-page.php,然后将page.php中的所有代码复制到tougao-page.php中;

2、删除tougao-page.php开头的所有注释,即 /* 与 */ ,以及它们之间的所有内容;

3、搜索:the_content,可以查找到类似代码<?php the_content(); ?>,将其替换成代码一

如果你在tougao-page.php中找不到the_content,那么你可以查找:get_template_part,可找到类似代码:<?php get_template_part( 'content', 'page' ); ?>,将content-page.php中的所有代码替换这部分代码即可。再用下面的代码替换<?php the_content(); ?>

代码一:

  1. <?php the_content(); ?>
  2.  
  3. <!– 关于表单样式,请自行调整–>
  4. <form class=”ludou-tougao” method=”post” action=”<?php echo $_SERVER[“REQUEST_URI”]; $current_user = wp_get_current_user(); ?>“>
  5.     <div style=textalign: left; paddingtop: 10px;>
  6.         <label for=“tougao_authorname”>昵称:*</label>
  7.         <input type=”text” size=”40″ value=”<?php if ( 0 != $current_user->ID ) echo $current_user->user_login; ?>” id=”tougao_authorname” name=”tougao_authorname” />
  8.     </div>
  9.  
  10.     <div style=textalign: left; paddingtop: 10px;>
  11.         <label for=“tougao_authoremail”>E-Mail:*</label>
  12.         <input type=”text” size=”40″ value=”<?php if ( 0 != $current_user->ID ) echo $current_user->user_email; ?>” id=”tougao_authoremail” name=”tougao_authoremail” />
  13.     </div>
  14.                      
  15.     <div style=textalign: left; paddingtop: 10px;>
  16.         <label for=“tougao_authorblog”>您的博客:</label>
  17.         <input type=”text” size=”40″ value=”<?php if ( 0 != $current_user->ID ) echo $current_user->user_url; ?>” id=”tougao_authorblog” name=”tougao_authorblog” />
  18.     </div>
  19.  
  20.     <div style=textalign: left; paddingtop: 10px;>
  21.         <label for=“tougao_title”>文章标题:*</label>
  22.         <input type=“text” size=“40” value=“” id=“tougao_title” name=“tougao_title” />
  23.     </div>
  24.  
  25.     <div style=textalign: left; paddingtop: 10px;>
  26.         <label for=“tougaocategorg”>分类:*</label>
  27.         <?php wp_dropdown_categories(‘hide_empty=0&id=tougaocategorg&show_count=1&hierarchical=1’); ?>
  28.     </div>
  29.                      
  30.     <div style=textalign: left; paddingtop: 10px;>
  31.         <label style=verticalalign:top for=“tougao_content”>文章内容:*</label>
  32.         <textarea rows=“15” cols=“55” id=“tougao_content” name=“tougao_content”></textarea>
  33.     </div>
  34.                      
  35.     <br clear=“all”>
  36.     <div style=textalign: center; paddingtop: 10px;>
  37.         <input type=“hidden” value=“send” name=“tougao_form” />
  38.         <input type=“submit” value=“提交” />
  39.         <input type=“reset” value=“重填” />
  40.     </div>
  41. </form>

二、添加表单处理代码

在tougao-page.php开头处中,将第一个 <?php 改成代码二:

  1. <?php
  2. /**
  3.  * 
  4.  * 更新记录
  5.  *  2010年09月09日 :
  6.  *  首个版本发布
  7.  *  
  8.  *  2011年03月17日 :
  9.  *  修正时间戳函数,使用wp函数current_time(‘timestamp’)替代time()
  10.  *  
  11.  *  2011年04月12日 :
  12.  *  修改了wp_die函数调用,使用合适的页面title
  13.  *  
  14.  *  2013年01月30日 :
  15.  *  错误提示,增加点此返回链接
  16.  *  
  17.  *  2013年07月24日 :
  18.  *  去除了post type的限制;已登录用户投稿不用填写昵称、email和博客地址
  19.  *  
  20.  *  2015年03月08日 :
  21.  *  使用date_i18n(‘U’)代替current_time(‘timestamp’)
  22.  */
  23.      
  24. if( isset($_POST[‘tougao_form’]) && $_POST[‘tougao_form’] == ‘send’) {
  25.     global $wpdb;
  26.     $current_url = ‘http://你的投稿页面地址’;   // 注意修改此处的链接地址
  27.     $last_post = $wpdb->get_var(“SELECT `post_date` FROM `$wpdb->posts` ORDER BY `post_date` DESC LIMIT 1”);
  28.     // 博客当前最新文章发布时间与要投稿的文章至少间隔120秒。
  29.     // 可自行修改时间间隔,修改下面代码中的120即可
  30.     // 相比Cookie来验证两次投稿的时间差,读数据库的方式更加安全
  31.     if ( (date_i18n(‘U’)  strtotime($last_post)) < 120 ) {
  32.         wp_die(‘您投稿也太勤快了吧,先歇会儿!<a href=”‘.$current_url.‘”>点此返回</a>’);
  33.     }
  34.          
  35.     // 表单变量初始化
  36.     $name = isset( $_POST[‘tougao_authorname’] ) ? trim(htmlspecialchars($_POST[‘tougao_authorname’], ENT_QUOTES)) : ;
  37.     $email =  isset( $_POST[‘tougao_authoremail’] ) ? trim(htmlspecialchars($_POST[‘tougao_authoremail’], ENT_QUOTES)) : ;
  38.     $blog =  isset( $_POST[‘tougao_authorblog’] ) ? trim(htmlspecialchars($_POST[‘tougao_authorblog’], ENT_QUOTES)) : ;
  39.     $title =  isset( $_POST[‘tougao_title’] ) ? trim(htmlspecialchars($_POST[‘tougao_title’], ENT_QUOTES)) : ;
  40.     $category =  isset( $_POST[‘cat’] ) ? (int)$_POST[‘cat’] : 0;
  41.     $content =  isset( $_POST[‘tougao_content’] ) ? trim(htmlspecialchars($_POST[‘tougao_content’], ENT_QUOTES)) : ;
  42.      
  43.     // 表单项数据验证
  44.     if ( empty($name) || mb_strlen($name) > 20 ) {
  45.         wp_die(‘昵称必须填写,且长度不得超过20字。<a href=”‘.$current_url.‘”>点此返回</a>’);
  46.     }
  47.      
  48.     if ( empty($email) || strlen($email) > 60 || !preg_match(“/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix”, $email)) {
  49.         wp_die(‘Email必须填写,且长度不得超过60字,必须符合Email格式。<a href=”‘.$current_url.‘”>点此返回</a>’);
  50.     }
  51.      
  52.     if ( empty($title) || mb_strlen($title) > 100 ) {
  53.         wp_die(‘标题必须填写,且长度不得超过100字。<a href=”‘.$current_url.‘”>点此返回</a>’);
  54.     }
  55.      
  56.     if ( empty($content) || mb_strlen($content) > 3000 || mb_strlen($content) < 100) {
  57.         wp_die(‘内容必须填写,且长度不得超过3000字,不得少于100字。<a href=”‘.$current_url.‘”>点此返回</a>’);
  58.     }
  59.      
  60.     $post_content = ‘昵称: ‘.$name.‘<br />Email: ‘.$email.‘<br />blog: ‘.$blog.‘<br />内容:<br />’.$content;
  61.      
  62.     $tougao = array(
  63.         ‘post_title’ => $title, 
  64.         ‘post_content’ => $post_content,
  65.         ‘post_category’ => array($category)
  66.     );
  67.     // 将文章插入数据库
  68.     $status = wp_insert_post( $tougao );
  69.    
  70.     if ($status != 0) { 
  71.         // 投稿成功给博主发送邮件
  72.         // somebody#example.com替换博主邮箱
  73.         // My subject替换为邮件标题,content替换为邮件内容
  74.         wp_mail(“somebody#example.com”,“My subject”,“content”);
  75.         wp_die(‘投稿成功!感谢投稿!<a href=”‘.$current_url.‘”>点此返回</a>’, ‘投稿成功’);
  76.     }
  77.     else {
  78.         wp_die(‘投稿失败!<a href=”‘.$current_url.‘”>点此返回</a>’);
  79.     }
  80. }

最后以UTF-8编码保存tougao-page.php,否则中文可能会乱码。然后进入WordPress管理后台 – 页面 – 创建页面,标题为投稿(可以自己起名),内容填上投稿说明等,右侧可以选择模板,选择 tougao 即可。此页面即前台注册页面,将该页面的链接放到网站任何位置,供用户点击注册即可。

好了,基本的投稿功能已经添加完毕,至于表单样式不好看,表单缺少你想要的项目等问题,你就自己添加css、表单项吧。

代码补充说明

1、如果你想让投稿的文章立即发布,而不需要审核再编辑,那么请将以上代码中的:

  1. ‘post_content’ => $post_content,

改成:

  1. ‘post_content’ => $post_content,‘post_status’ => ‘publish’,

2、如果你想给投稿页面增加验证码功能,可以 WordPress添加投稿功能的方法-小小资源铺点此下载.zip 验证码文件,解压后将captcha目录放到当前主题目录下,然后在代码一中,将35行的:

  1. <br clear=“all”>

改成:

  1. <div style=textalign: left; paddingtop: 10px;>
  2.   <label for=“CAPTCHA”>验证码:
  3.     <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>
  4.   </label>
  5. </div>
  6.      
  7. <div style=textalign: left; paddingtop: 10px;>
  8.   <label>
  9.     <img id=”captcha_img” src=”<?php bloginfo(‘template_url’); ?>/captcha/captcha.php” />
  10.   </label>
  11. </div>
  12.             
  13. <br clear=“all”>

将代码二中的:

  1. if( isset($_POST[‘tougao_form’]) && $_POST[‘tougao_form’] == ‘send’) {

改成:

  1. if (!isset($_SESSION)) {
  2.  session_start();
  3. session_regenerate_id(TRUE);
  4. }
  5.   
  6. if( isset($_POST[‘tougao_form’]) && $_POST[‘tougao_form’] == ‘send’) {
  7.   if(empty($_POST[‘captcha_code’])
  8.     || empty($_SESSION[‘ludou_lcr_secretword’])
  9.     || (trim(strtolower($_POST[‘captcha_code’])) != $_SESSION[‘ludou_lcr_secretword’])
  10.   ) {
  11.     wp_die(‘验证码不正确!<a href=”‘.$current_url.‘”>点此返回</a>’);
  12.   }
免责声明:
使用本站资源的用户均应仔细阅读本声明。用户使用本站资源的行为将被视为对本声明全部内容的认可。
1、本站资源大部分搜集于网络,整理并分享。用户可自行搜索在其他地方下载,也可选择在本站下载。如果侵犯了您的合法权益,请联系:diy945945@111.com 及时删除。本站资源仅用于研究、学习之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。
2、本站对发布的资源不能保证其完整性、安全性和可用性。请您在下载后自行检查。您在使用过程中遇到的任何问题与本站无关。
3、注册本站以及在本站充值、开通会员等消费行为仅作为用户本人对本站的友情赞助,均为用户本人的自愿行为。相当于您是自愿赞助本站的服务器以及运营维护费用,而不是购买本站的任何服务与资源,请知悉!!!
4、本站资源资源采用网盘分享,如链接失效,请及时联系。
5、原文链接:https://www.xxziyuan.top/3401.html,转载请注明出处。
0

评论0

请先
M3U8视频批量下载PC工具,支持ts格式合并为MP4(v2.0.7)
M3U8视频批量下载PC工具,支持ts格式合并为MP4(v2.0.7)
3分钟前 有人购买 去瞅瞅看

站点公告

专注WordPress-源码-实用软件分享!

WordPress网站搭建联系qq:429413218

显示验证码
没有账号?注册  忘记密码?