wordpress 非插件實(shí)現(xiàn)前臺(tái)投稿功能

字號(hào):


    很多網(wǎng)站都想開(kāi)放讀者的投稿功能,接受讀者的投稿,不僅可以豐富博客的內(nèi)容,還可以增加與讀者之間的溝通,可以說(shuō)是一舉多得的事情,何樂(lè)不為呢?wordpress本身并不提供投稿功能,但是wordpress擁有強(qiáng)大的擴(kuò)展能力,我們可以自己來(lái)添加這個(gè)功能。
    實(shí) 現(xiàn)用戶投稿,有兩種方法,一種是開(kāi)放后臺(tái)的注冊(cè)功能,普通用戶注冊(cè)進(jìn)去默認(rèn)設(shè)置為投稿者,登陸進(jìn)去即可添加文章(默認(rèn)為草稿);另一種方法是在前臺(tái)提供投 稿表單,用戶填寫相應(yīng)的表格即可。前一種方法實(shí)現(xiàn)起來(lái)比較簡(jiǎn)單,基本不需要博主配置太多東西,只是有些博主可能會(huì)覺(jué)得別扭,不愿讓他人看到自己的博客后 臺(tái);而后一種方法對(duì)投稿者來(lái)說(shuō)方便了很多,博主也不用擔(dān)心自己博客的后臺(tái)隱私,只是該方法實(shí)現(xiàn)起來(lái)比較麻煩,需要配置的東西很多。本文也只將介紹后一種方 法,希望對(duì)你有所幫助,當(dāng)然也只是復(fù)制粘貼代碼就可以了。
    一、添加投稿表單
    1、首先在當(dāng)前主題的目錄下新建一個(gè)php文件,命名為tougao-page.php,然后將page.php中的所有代碼復(fù)制到tougao-page.php中;
    2、刪除tougao-page.php開(kāi)頭的所有注釋,即 /* 與 */ ,以及它們之間的所有內(nèi)容;
    3、將 <?php the_content(); ?> 改成以下代碼:
    <?php the_content(); ?>
    <!-- 關(guān)于表單樣式,請(qǐng)自行調(diào)整-->
    <form method=post action=<?php echo $_server[request_uri]; ?>>
    <div style=text-align: left; padding-top: 10px;>
    <label for=tougao_authorname>昵稱:*</label>
    <input type=text size=40 value= id=tougao_authorname name=tougao_authorname />
    </div>
    <div style=text-align: left; padding-top: 10px;>
    <label for=tougao_authoremail>e-mail:*</label>
    <input type=text size=40 value= id=tougao_authoremail name=tougao_authoremail />
    </div>
    <div style=text-align: left; padding-top: 10px;>
    <label for=tougao_authorblog>您的博客:</label>
    <input type=text size=40 value= id=tougao_authorblog name=tougao_authorblog />
    </div>
    <div style=text-align: left; padding-top: 10px;>
    <label for=tougao_title>文章標(biāo)題:*</label>
    <input type=text size=40 value= id=tougao_title name=tougao_title />
    </div>
    <div style=text-align: left; padding-top: 10px;>
    <label for=tougaocategorg>分類:*</label>
    <?php wp_dropdown_categories('id=tougaocategorg&show_count=1&hierarchical=1'); ?>
    </div>
    <div style=text-align: left; padding-top: 10px;>
    <label for=tougao_content>文章內(nèi)容:*</label>
    <textarea rows=15 cols=55 id=tougao_content 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開(kāi)頭處中,將第一個(gè) <?php 改成:
    <?php
    /**
    * template name: tougao
    * 作者:露兜
    * 博客:
    * 
    * 更新記錄
    * 2010年09月09日 :
    * 首個(gè)版本發(fā)布
    * 
    * 2011年03月17日 :
    * 修正時(shí)間戳函數(shù),使用wp函數(shù)current_time('timestamp')替代time()
    * 
    * 2011年04月12日 :
    * 修改了wp_die函數(shù)調(diào)用,使用合適的頁(yè)面title
    */
    if( isset($_post['tougao_form']) && $_post['tougao_form'] == 'send')
    {
    global $wpdb;
    $last_post = $wpdb->get_var(select post_date from $wpdb->posts where post_type = 'post' order by post_date desc limit 1);
    // 博客當(dāng)前最新文章發(fā)布時(shí)間與要投稿的文章至少間隔120秒。
    // 可自行修改時(shí)間間隔,修改下面代碼中的120即可
    // 相比cookie來(lái)驗(yàn)證兩次投稿的時(shí)間差,讀數(shù)據(jù)庫(kù)的方式更加安全
    if ( current_time('timestamp') - strtotime($last_post) < 120 )
    {
    wp_die('您投稿也太勤快了吧,先歇會(huì)兒!');
    }
    // 表單變量初始化
    $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)) : '';
    // 表單項(xiàng)數(shù)據(jù)驗(yàn)證
    if ( empty($name) || mb_strlen($name) > 20 )
    {
    wp_die('昵稱必須填寫,且長(zhǎng)度不得超過(guò)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必須填寫,且長(zhǎng)度不得超過(guò)60字,必須符合email格式');
    }
    if ( empty($title) || mb_strlen($title) > 100 )
    {
    wp_die('標(biāo)題必須填寫,且長(zhǎng)度不得超過(guò)100字');
    }
    if ( empty($content) || mb_strlen($content) > 3000 || mb_strlen($content) < 100)
    {
    wp_die('內(nèi)容必須填寫,且長(zhǎng)度不得超過(guò)3000字,不得少于100字');
    }
    $post_content = '昵稱: '.$name.'<br />email: '.$email.'<br />blog: '.$blog.'<br />內(nèi)容:<br />'.$content;
    $tougao = array(
    'post_title' => $title,
    'post_content' => $post_content,
    'post_category' => array($category)
    );
    // 將文章插入數(shù)據(jù)庫(kù)
    $status = wp_insert_post( $tougao );
    if ($status != 0) 
    { 
    // 投稿成功給博主發(fā)送郵件
    // somebody#example.com替換博主郵箱
    // my subject替換為郵件標(biāo)題,content替換為郵件內(nèi)容
    wp_mail(somebody#example.com,my subject,content);
    wp_die('投稿成功!感謝投稿!', '投稿成功');
    }
    else
    {
    wp_die('投稿失?。?);
    }
    }
    最后以u(píng)tf-8編碼保存tougao-page.php,否則中文可能會(huì)亂碼。
    代碼補(bǔ)充說(shuō)明,如果你想讓投稿的文章立即發(fā)布,而不需要審核再編輯,那么請(qǐng)將以上代碼58行改成:
    'post_content' => $post_content, 'post_status' => 'publish', 
    最后進(jìn)入wordpress管理后臺(tái) – 頁(yè)面 – 創(chuàng)建頁(yè)面,標(biāo)題為投稿(可以自己起名),內(nèi)容填上投稿說(shuō)明等,右側(cè)可以選擇模板,選擇 tougao 即可。
    好了,基本的投稿功能已經(jīng)添加完畢,至于表單樣式不好看,表單缺少你想要的項(xiàng)目等問(wèn)題,你就自己添加css、表單項(xiàng)吧。