VPS参考、测评、推荐
分享你关注的VPS主机优惠信息

详细讲解一下WordPress中添加投稿功能的方法。

详细讲解一下WordPress中添加投稿功能的方法。

详细讲解一下WordPress中添加投稿功能的方法。

很多网站都想开放读者的投稿功能,接受读者的投稿,不仅可以丰富博客的内容,还可以增加与读者之间的沟通,可以说是一举多得的事情,何乐不为呢? & # 26412身并不提供投稿功能,但是 & # 25317有强大的扩展能力,我们可以自己来添加这个功能。

实现用户投稿,有两种方法,一种是开放后台的注册功能,普通用户注册进去默认设置为投稿者,登陆进去即可添加文章(默认为草稿);另一种方法是在前台提供投稿表单,用户填写相应的表格即可。前一种方法实现起来比较简单,基本不需要博主配置太多东西,只是有些博主可能会觉得别扭,不愿让他人看到自己的博客后台;而后一种方法对投稿者来说方便了很多,博主也不用担心自己博客的后台隐私,只是该方法实现起来比较麻烦,需要配置的东西很多。本文也只将介绍后一种方法,希望对你有所帮助,当然也只是复制粘贴代码就可以了。

一、添加投稿表单1、首先在当前主题的目录下新建一个 & # 25991件,命名为tougao-page . & # 65292;然后将page.php & # 20013的所有代码复制到tougao-page . PHP & # 20013;;

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

3、搜索:content & # 65292可以查找到类似代码& lt?PHP the _ content();?& gt,将其替换成代码一

如果你在tougao-page . PHP & # 20013;找不到_ content & # 65292那么你可以查找:get _ template _ part & # 65292可找到类似代码:& lt?php get_template_part( ‘content ‘,’ page ‘);?& gt,将content-page . PHP & # 20013;的所有代码替换这部分代码即可。再用下面的代码替换& lt?PHP the _ content();?& gt

代码一:

& lt?PHP the _ content();?& gt& lt!- 关于表单样式,请自行调整-& gt;& ltform class = ” ludou-tougao ” method = ” post ” action = ” & lt;?php echo $_SERVER[“请求_ URI”];$ current _ user = _ get _ current _ user();?>& gt& ltdiv style = ” text-align:left;填充顶部:10px”& gt& lt=”tougao_authorname ” >的标签昵称:* & lt/label & gt;& ltinput type = ” text ” size = ” 40 ” value = ” & lt;?php if ( 0!= $ current _ user-& gt;ID)echo $ current _ user-& gt;user _ login?>id = ” tougao _ author name ” name = ” tougao _ author name “/& gt;& lt/div & gt;& ltdiv style = ” text-align:left;填充顶部:10px”& gt& lt=”tougao_authoremail ” >的标签。电子邮件:* & lt/label & gt;& ltinput type = ” text ” size = ” 40 ” value = ” & lt;?php if ( 0!= $ current _ user-& gt;ID)echo $ current _ user-& gt;user _ ?>id = ” tougao _ author ” name = ” tougao _ author email “/& gt;& lt/div & gt;& ltdiv style = ” text-align:left;填充顶部:10px”& gt& lt=”tougao_authorblog ” >的标签。您的博客:& lt/label & gt;& ltinput type = ” text ” size = ” 40 ” value = ” & lt;?php if ( 0!= $ current _ user-& gt;ID)echo $ current _ user-& gt;user _ url?>id = ” tougao _ author blog ” name = ” tougao _ author blog “/& gt;& lt/div & gt;& ltdiv style = ” text-align:left;填充顶部:10px”& gt& lt标签为=”tougao_title ” >文章标题:* & lt/label & gt;& ltinput type = ” text ” size = ” 40 ” value = ” ” id = ” tougao _ title ” name = ” tougao _ title “/& gt;& lt/div & gt;& ltdiv style = ” text-align:left;填充顶部:10px”& gt& lt标签为=”tougaocategorg ” >分类:* & lt/label & gt;& lt?PHP _ drop down _ categories(‘ hide _ empty = 0 & amp;id=tougaocategorg&amp。show _ count = 1 & amphierarchical = 1’);?& gt& lt/div & gt;& ltdiv style = ” text-align:left;填充顶部:10px”& gt& ltlabel style = ” vertical-align:top ” for = ” tougao _ content ” >文章内容:* & lt/label & gt;& lttextarea rows = ” 15 ” cols = ” 55 ” id = ” tougao _ content ” name = ” tougao _ content ” & gt;& lt/textarea & gt;& lt/div & gt;& ltbr clear=”all ” >& ltdiv style = ” text-align:center;填充顶部:10px”& gt& ltinput type = ” hidden ” value = ” send ” name = ” tougao _ form “/& gt;& lt输入type = ” submit ” value = ” & # 25552交”/& gt;& lt输入type = ” reset ” value = ” & # 37325填”/& gt;& lt/div & gt;& lt/form & gt;二、添加表单处理代码在tougao-page . PHP & # 24320;头处中,将第一个& lt?php & # 25913成代码二:

& lt?php/** *名称:tougao * & # 20316者:露兜* 博客:://www.ludou.org/ * * & # 26356;新记录* 2010年09月09日:* 首个版本发布* * 2011年03月17日:* 修正时间戳函数,使用wp & # 20989数当前时间(‘时间戳’)& # 26367;代时间()* * 2011 & # 24180;04月12日:* 修改了wp _ die & # 20989数调用,使用合适的页面标题* * 2013 & # 24180;01月30日:* 错误提示,增加点此返回链接* * 2013年07月24日:* 去除了帖子类型& # 30340;限制;已登录用户投稿不用填写昵称、电子邮件& # 21644;博客地址* * 2015年03月08日:* 使用date _ i18n(‘ U ‘)& # 20195;替current _ time(‘ timestamp ‘)*/if(isset($ _ POST[‘ tougao _ form ‘])& amp;& amp$ _ POST[‘ tougao _ form ‘]= ‘ send ‘){ global $ wpdb;$ current _ URL = ‘ http://& # 20320;的投稿页面地址’;// 注意修改此处的链接地址$ last _ post = $ wpdb-& gt;get _ var(” SELECT ` post _ date ‘ FROM `$wpdb-& gt;post ` ORDER BY ` post _ date ` desc限制1 “);// 博客当前最新文章发布时间与要投稿的文章至少间隔120秒。// 可自行修改时间间隔,修改下面代码中的120即可// 相比饼干& # 26469;验证两次投稿的时间差,读数据库的方式更加安全if((date _ i18n(‘ U ‘)-strtotime($ last _ post))& lt;120){ WP _ die(‘ & # 24744;投稿也太勤快了吧,先歇会儿!& lta href= ” “。$current_url。” & gt点此返回& lt/a & gt;’ );} // 表单变量初始化$ name = isset($ _ POST[‘ tougao _ author name ‘])?trim(htmlspecialchars($ _ POST[‘ tougao _ author name ‘],ENT _ QUOTES)):’ ‘;$ email = isset($ _ POST[‘ tougao _ author email ‘])?trim(htmlspecialchars($ _ POST[‘ tougao _ author email ‘],ENT _ QUOTES)):’ ‘;$ blog = isset($ _ POST[‘ tougao _ author blog ‘])?trim(htmlspecialchars($ _ POST[‘ tougao _ author blog ‘],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(‘ & # 26165;称必须填写,且长度不得超过20字。& lta href= ” “。$current_url。” & gt点此返回& lt/a & gt;’ );} 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 & # 24517;须填写,且长度不得超过60字,必须符合电子邮件& # 26684;式。& lta href= ” “。$current_url。” & gt点此返回& lt/a & gt;’ );} if(empty($ title)| | MB _ strlen($ title)& gt;100){ WP _ die(‘ & # 26631;题必须填写,且长度不得超过100字。& lta href= ” “。$current_url。” & gt点此返回& lt/a & gt;’ );} if(empty($ content)| | MB _ strlen($ content)& gt;3000 | | MB _ strlen($ content)& lt;100){ WP _ die(‘ & # 20869;容必须填写,且长度不得超过3000字,不得少于100字。& lta href= ” “。$current_url。” & gt点此返回& lt/a & gt;’ );} $ post _ content = ‘ & # 26165称: ‘.$name。& ltbr/>;电子邮件:’。$电子邮件。& ltbr/>;:’。$。& ltbr/>;内容:& ltbr/>;。$内容;$ tougao = array(‘ post _ title ‘ = & gt;$title,’ post _ content ‘ = & gt$post_content,’ post _ category ‘ = & gt数组($ category));// 将文章插入数据库$ status = WP _ insert _ post($ tougao);如果($状态!= 0) { // 投稿成功给博主发送邮件//someone # example . & # 26367;换博主邮箱//我的主题& # 26367;换为邮件标题,内容& # 26367;换为邮件内容WP _ mail(” someone # example . “、”我的主题”、”内容”);WP _ die(‘ & # 25237;稿成功!感谢投稿!& lta href= ” “。$current_url。” & gt点此返回& lt/a & gt;’ , ‘投稿成功’ );} else { WP _ die(‘ & # 25237;稿失败!& lta href= ” “。$current_url。” & gt点此返回& lt/a & gt;’ );}}最后以UTF-8 & # 32534;码保存tougao-page . PHP & # 65292;否则中文可能会乱码。然后进入WordPress & # 31649理后台& ndash页面& ndash创建页面,标题为投稿(可以自己起名),内容填上投稿说明等,右侧可以选择模板,选择透高& # 21363;可。此页面即前台注册页面,将该页面的链接放到网站任何位置,供用户点击注册即可。

好了,基本的投稿功能已经添加完毕,至于表单样式不好看,表单缺少你想要的项目等问题,你就自己添加css & # 12289表单项吧。最后,也欢迎给本站投稿哦,当然本站的投稿方式是开放后台的注册功能,不是以上的表单形式。

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

‘ post _ content ‘ = & gt$post_content,& # 25913;成:

‘ post _ content ‘ = & gt$post_content,’ post _ status ‘ = & gt发布’,2 & # 12289;如果你想让用户在投稿的同时,将投稿者注册成你本站的投稿者,并将文章的作者归到这个投稿者的名下,你可以参考此条回复的内容修改相应的代码:查看回复。

3、如果你的博客文章都有自定义栏目,并且想在用户投稿的同时添加自定义栏目,可以参考这条回复:查看回复。

4、如果你觉得本文提供的文章编辑框太过单调,需要一个富文本编辑,你可以看看这篇文章(包含图片上传功能):WordPress & # 25237稿功能添加富文本编辑器

5、如果你使用了一些富文本编辑器,文章提交后内容中的代码都被转义了,可以参考这条回复:查看回复。

6、如果你需要投稿的文章发布后通知投稿者,可以看看这篇文章(前提投稿的文章默认是草稿状态,而不是直接发布):WordPress & # 25237稿功能添加邮件提醒功能

7、如果你想给投稿页面增加验证码功能,可以点此下载验证码文件,解压后将验证码& # 30446;录放到当前主题目录下,然后在代码一中,将35行的:

& ltbr clear=”all ” >改成:

& ltdiv style = ” text-align:left;填充顶部:10px”& gt& lt=”CAPTCHA ” >的标签。验证码:& ltinput id = ” CAPTCHA ” style = ” width:110 px;*浮动:左;”class = ” input ” type = ” text ” tabindex = ” 24 ” size = ” 10 ” value = ” ” name = ” captcha _ code “/& gt;看不清?& lta href = ” JavaScript:void(0)” onclick = ” document . getelementbyid(‘ captcha _ img ‘)。src = ‘ & lt?PHP bloginfo(‘ template _ URL ‘);?& gt/captcha/captcha.php?’+math . random();document . getelementbyid(‘ CAPTCHA ‘)。焦点();返回false” & gt点击更换& lt/a & gt;& lt/label & gt;& lt/div & gt;& ltdiv style = ” text-align:left;填充顶部:10px” & gt& lt标签& gt& ltimg id = ” captcha _ img ” src = ” & lt?PHP bloginfo(‘ template _ URL ‘);?& gt/captcha/captcha . PHP “/& gt;& lt/label & gt;& lt/div & gt;& ltbr clear=”all ” >将代码二中的:

if(isset($ _ POST[‘ tougao _ form ‘])& amp;& amp$ _ POST[‘ tougao _ form ‘]= = ‘ send ‘){ & # 25913;成:

如果(!isset($ _ SESSION)){ SESSION _ start();session _ regenerate _ id(TRUE);} if(isset($ _ POST[‘ tougao _ form ‘])& amp;& amp$ _ POST[‘ tougao _ form ‘]= ‘ send ‘){ if(empty($ _ POST[‘ captcha _ code ‘])| | empty($ _ SESSION[‘ ludou _ LCR _ secret world ‘])| |(trim(strtolower($ _ POST[‘ captcha _ code ‘]))!= $ _ SESSION[‘ ludou _ LCR _ secret world ‘]){ WP _ die(‘ & # 39564;证码不正确!& lta href= ” “。$current_url。” & gt点此返回& lt/a & gt;’ );}大功造成!

推荐学习:《WordPress & # 25945程》

在WordPress中投稿功能的详细内容。更多请关注草根吧VPS其他相关文章!

:详细讲解一下WordPress中投稿功能的://vps.caogenba.com.com/94656.html

赞(0) 打赏
未经允许不得转载:草根吧VPS_最新VPS信息参考 » 详细讲解一下WordPress中添加投稿功能的方法。
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址