WordPress中如何添加投稿功能

69次阅读
没有评论

共计 4166 个字符,预计需要花费 11 分钟才能阅读完成。

这篇文章主要讲解了“WordPress 中如何添加投稿功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着丸趣 TV 小编的思路慢慢深入,一起来研究和学习“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(); ?

代码一:

?php the_content(); ? 
 !--  关于表单样式,请自行调整 -- 
 form  >

二、添加表单处理代码

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

 ?php
 * Template Name: tougao
 *  作者:露兜
 *  博客:https://www.ludou.org/
 * 
 *  更新记录
 * 2010 年 09 月 09 日  : *  首个版本发布
 * 
 * 2011 年 03 月 17 日  : *  修正时间戳函数,使用 wp 函数 current_time(timestamp)替代 time()
 * 
 * 2011 年 04 月 12 日  : *  修改了 wp_die 函数调用,使用合适的页面 title
 * 
 * 2013 年 01 月 30 日  : *  错误提示,增加点此返回链接
 * 
 * 2013 年 07 月 24 日  : *  去除了 post type 的限制;已登录用户投稿不用填写昵称、email 和博客地址
 * 
 * 2015 年 03 月 08 日  : *  使用 date_i18n(U)代替 current_time(timestamp)
 */
 
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 ( (date_i18n( U) - 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 
 }
}

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

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

代码补充说明

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

post_content = $post_content,

改成:

post_content = $post_content, post_status = publish ,

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

br clear= all 

改成:

 div >   label for= CAPTCHA 验证码:
    input id= CAPTCHA >   /label
/div
   
div >   label
    img id= captcha_img src= ?php bloginfo(template_url ? /captcha/captcha.php /
  /label
/div
         
br clear= all

将代码二中的:

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

改成:

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 中如何添加投稿功能”的内容了,经过本文的学习后,相信大家对 WordPress 中如何添加投稿功能这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是丸趣 TV,丸趣 TV 小编将为大家推送更多相关知识点的文章,欢迎关注!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-12发表,共计4166字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)