现在的位置: 首页Wordpress>正文
WordPress实现投稿功能
2010年10月11日 Wordpress 评论数 3 ⁄ 被围观 1,063 views+

一、添加投稿表单

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

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

3、将 <?php the_content(); ?> 改成以下代码:

<?php the_content(); ?>

<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
    <div style="text-align: left; padding-top: 10px;">
        <label>昵称:*</label>
    </div>
    <div>
        <input type="text" size="40" value="" name="tougao_authorname" />
    </div>

    <div style="text-align: left; padding-top: 10px;">
        <label>E-Mail:*</label>
    </div>
    <div>
        <input type="text" size="40" value="" name="tougao_authoremail" />
    </div>
                   
    <div style="text-align: left; padding-top: 10px;">
        <label>您的博客:</label>
    </div>
    <div>
        <input type="text" size="40" value="" name="tougao_authorblog" />
    </div>
                    
    <div style="text-align: left; padding-top: 10px;">
        <label>文章标题:*</label>
    </div>
    <div>
        <input type="text" size="40" value="" name="tougao_title" />
    </div>

    <div style="text-align: left; padding-top: 10px;">
        <label>分类:*</label>
    </div>
    <div style="text-align: left;">
        <?php wp_dropdown_categories('show_count=1&hierarchical=1'); ?>
    </div>
                   
    <div style="text-align: left; padding-top: 10px;">
        <label>文章内容:*</label>
    </div>
    <div>
        <textarea rows="15" cols="55" name="tougao_content"></textarea>
    </div>
                   
    <br clear="all">
    <div style="text-align: center; padding-top: 10px;">
        <input type="hidden" value="send" name="tougao_form" />
        <input type="submit" value="提交" />
        <input type="reset" value="重填" />
    </div>
</form>

二、添加表单处理代码

在tougao-page.php中,将第一个 <?php 改成:

<?php
/*
 * Template Name: tougao
 * @author: Ludou  
 * @Email : zhouzb889@gmail.com
 * @Blog  : http://www.ludou.org/
 */
   
if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == 'send')
{
    if ( isset($_COOKIE["tougao"]) && ( time() - $_COOKIE["tougao"] ) < 120 )
    {
        wp_die('您投稿也太勤快了吧,先歇会儿!');
    }
       
    // 表单变量初始化
    $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) || strlen($name) > 20 )
    {
        wp_die('昵称必须填写,且长度不得超过20字');
    }
   
    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格式');
    }
   
    if ( empty($title) || strlen($title) > 100 )
    {
        wp_die('标题必须填写,且长度不得超过100字');
    }
   
    if ( empty($content) || strlen($content) > 3000 || strlen($content) < 100)
    {
        wp_die('内容必须填写,且长度不得超过3000字,不得少于100字');
    }
   
    $post_content = '昵称: '.$name.'<br />Email: '.$email.'<br />blog: '.$blog.'<br />内容:'.$content;
 
    $tougao = array(
        'post_title' => $title,
        'post_content' => $post_content,
        'post_category' => array($category)
    );

    // 将文章插入数据库
    $status = wp_insert_post( $tougao );
 
    if ($status != 0)
    {
        setcookie("tougao", time(), time()+180);
        wp_die('投稿成功!感谢投稿!');
    }
    else
    {
        wp_die('投稿失败!');
    }
}

 

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

'post_content' => $post_content, 'post_status' => 'publish',

 

最后进入WordPress管理后台 – 页面 – 创建页面,标题为投稿(可以自己起名),内容填上投稿说明等,右侧可以选择模板,选择 tougao 即可好了,基本的投稿功能已经添加完毕,至于表单样式不好看,表单缺少你想要的项目等问题,你就自己添加css、表单项吧。

文章来源:http://www.ludou.org/wordpress-add-contribute-page.html

本文作者:Ludou

目前有 3 条留言 其中:访客:2 条, 博主:1 条

  1. admin : 2010年10月11日17:47:16  -49楼 @回复 回复

    有时间我也弄个试试。呵呵~ :mrgreen:

  2. 优得客 : 2010年10月12日20:44:50  -48楼 @回复 回复

    这个很好,谢谢了,但是估计我的博客现在还没有人会投稿的。 :evil: :evil: :evil:

  3. 优得客 : 2010年10月13日11:00:42  -47楼 @回复 回复

    刚刚测试了一个,居然没有成功,

给我留言

留言无头像?


泉州SEO