首页 > Wordpress > WordPress无插件实现游客投稿功能
2014
07-14

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

作者笔名:郑力
作者邮箱:admin@zlphp.com
作者主页:http://www.zlphp.com
文章内容:
1、复制主题文件page.php重命名为contribute.php。
2、在

  1. <?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. ?>    

3、在

  1. <?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>    

4、新建页面,页面模板选择“游客投稿”。

最后编辑:
作者:漱石
这个作者貌似有点懒,什么都没有留下。
捐 赠如果您觉得这篇文章有用处,请支持作者!鼓励作者写出更好更多的文章!

留下一个回复