码迷,mamicode.com
首页 > 其他好文 > 详细

WordPress无插件实现游客投稿功能

时间:2014-12-16 19:23:38      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:seo博客推荐   何碧玉的博客   seo优化学习   

WordPress不安装插件如何实现游客投稿功能?SEO博客推荐下面方法轻松实现。

首先,复制文件page.php重命名为contribute.php。

其次,将contribute.php文件中,在<?php get_header(); ?>前面添加如下代码:

  1. <?php

  2. /*

  3. Template Name:游客投稿

  4. */

  5. if(isset($_POST[‘contributeform‘])&&$_POST[‘contributeform‘]==‘send‘){

  6.     global $wpdb;

  7.     $last_post=$wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_type = ‘post‘ ORDER BY post_date DESC LIMIT 1");

  8.     if(current_time(‘timestamp‘)-strtotime($last_post)<60){wp_die(‘投稿间隔应至少为1分钟!‘);}

  9.     $name=isset($_POST[‘authorname‘])?trim(htmlspecialchars($_POST[‘authorname‘],ENT_QUOTES)):‘‘;

  10.     $email=isset($_POST[‘authoremail‘])?trim(htmlspecialchars($_POST[‘authoremail‘],ENT_QUOTES)):‘‘;

  11.     $url=isset($_POST[‘authorurl‘])?trim(htmlspecialchars($_POST[‘authorurl‘],ENT_QUOTES)):‘‘;

  12.     $title=isset($_POST[‘articletitle‘])? trim(htmlspecialchars($_POST[‘articletitle‘],ENT_QUOTES)):‘‘;

  13.     $category=isset($_POST[‘cat‘])?(int)$_POST[‘cat‘]:0;

  14.     $content=isset($_POST[‘articlecontent‘])?trim(htmlspecialchars($_POST[‘articlecontent‘],ENT_QUOTES)):‘‘;

  15.     if(emptyempty($name)||mb_strlen($name)>30){wp_die(‘作者笔名必须填写,且长度不能超过30个字符!‘);}

  16.     if(emptyempty($email)||strlen($email)>60||!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix",$email)){wp_die(‘作者邮箱必须填写,且长度不能超过60个字符,并符合邮箱格式!‘);}

  17.     if(emptyempty($title)||mb_strlen($title)>120){wp_die(‘文章标题必须填写,且长度不能超过120个字符!‘);}

  18.     if(emptyempty($content)||mb_strlen($content)>20000||mb_strlen($content)<100){wp_die(‘文章内容必须填写,且长度不能超过20000个字符,不能少于100个字符!‘);}

  19.     $post_content=‘作者笔名:‘.$name.‘<br />作者邮箱:‘.$email.‘<br />作者主页:‘.$url.‘<br />文章内容:<br />‘.$content;

  20.     $contribute=array(‘post_title‘=>$title,‘post_content‘=>$post_content,‘post_category‘=>array($category));

  21.     $status=wp_insert_post($contribute);

  22.     if($status!=0){

  23.       wp_mail(‘‘.get_option(‘blog_mail_username‘).‘‘,‘‘.get_option(‘blogname‘).‘游客投稿‘,‘您的‘.get_option(‘blogname‘).‘有游客投稿!‘);

  24.       wp_die(‘投稿成功,您的文章将在审核通过后发布!‘,‘投稿成功‘);}

  25.     else{wp_die(‘投稿失败‘);}}

  26. ?>

再次,在<?php the_content(); ?>后面添加如下代码:

  1. <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">

  2. <div class="contributebox">

  3.   <div class="inputclass"><input type="text" name="authorname" id="authorname" value="" /><label for="authorname">作者笔名(必填)</label></div>

  4.   <div class="inputclass"><input type="text" name="authoremail" id="authoremail" value="" /><label for="authoremail">作者邮箱(必填)</label></div>

  5.   <div class="inputclass"><input type="text" name="authorurl" id="authorurl" value="" /><label for="authorurl">作者主页</label></div>

  6.   <div class="inputclass"><input type="text" name="articletitle" id="articletitle" value="" /><label for="articletitle">文章标题(必填)</label></div>

  7.   <div class="inputclass"><?php wp_dropdown_categories(‘show_count=1&hierarchical=1‘); ?><label for="cat">文章分类(必选)</label></div>

  8.   <textarea name="articlecontent" id="articlecontent" onblur="if(this.innerHTML==‘‘){this.innerHTML=‘文章内容...‘;this.style.color=‘‘}" onFocus="if(this.innerHTML==‘文章内容...‘){this.innerHTML=‘‘;this.style.color=‘‘}">文章内容...</textarea>

  9.   <input type="hidden" value="send" name="contributeform" />

  10.   <input type="submit" class="contributesubmit" id="contributesubmit" value="确认投稿" />

  11. </div>

  12. </form>

最后,新建一个页面,模板选择“游客投稿”(contribute.php),就形成了一个投稿页面。


本文出自 “何碧玉的博客” 博客,请务必保留此出处http://hebiyu90.blog.51cto.com/9736895/1590772

WordPress无插件实现游客投稿功能

标签:seo博客推荐   何碧玉的博客   seo优化学习   

原文地址:http://hebiyu90.blog.51cto.com/9736895/1590772

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!