很多網(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、搜索: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(); ?> <!-- 關(guān)于表單樣式,請(qǐng)自行調(diào)整--> <form class="ludou-tougao" method="post" action="<?php echo $_SERVER["REQUEST_URI"]; $current_user = wp_get_current_user(); ?>"> <div style="text-align: left; padding-top: 10px;"> <label for="tougao_authorname">昵稱:*</label> <input type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_login; ?>" 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="<?php if ( 0 != $current_user->ID ) echo $current_user->user_email; ?>" 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="<?php if ( 0 != $current_user->ID ) echo $current_user->user_url; ?>" 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('hide_empty=0&id=tougaocategorg&show_count=1&hierarchical=1'); ?> </div> <div style="text-align: left; padding-top: 10px;"> <label style="vertical-align:top" 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 /** * * 更新記錄 * 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 * * 2013年01月30日 : * 錯(cuò)誤提示,增加點(diǎn)此返回鏈接 * * 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://你的投稿頁(yè)面地址'; // 注意修改此處的鏈接地址 $last_post = $wpdb->get_var("SELECT `post_date` FROM `$wpdb->posts` ORDER BY `post_date` DESC LIMIT 1"); // 博客當(dāng)前最新文章發(fā)布時(shí)間與要投稿的文章至少間隔120秒。 // 可自行修改時(shí)間間隔,修改下面代碼中的120即可 // 相比Cookie來(lái)驗(yàn)證兩次投稿的時(shí)間差,讀數(shù)據(jù)庫(kù)的方式更加安全 if ( (date_i18n('U') - strtotime($last_post)) < 120 ) { wp_die('您投稿也太勤快了吧,先歇會(huì)兒!<a href="'.$current_url.'">點(diǎn)此返回</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)) : ''; // 表單項(xiàng)數(shù)據(jù)驗(yàn)證 if ( empty($name) || mb_strlen($name) > 20 ) { wp_die('昵稱必須填寫,且長(zhǎng)度不得超過(guò)20字。<a href="'.$current_url.'">點(diǎn)此返回</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必須填寫,且長(zhǎng)度不得超過(guò)60字,必須符合Email格式。<a href="'.$current_url.'">點(diǎn)此返回</a>'); } if ( empty($title) || mb_strlen($title) > 100 ) { wp_die('標(biāo)題必須填寫,且長(zhǎng)度不得超過(guò)100字。<a href="'.$current_url.'">點(diǎn)此返回</a>'); } if ( empty($content) || mb_strlen($content) > 3000 || mb_strlen($content) < 100) { wp_die('內(nèi)容必須填寫,且長(zhǎng)度不得超過(guò)3000字,不得少于100字。<a href="'.$current_url.'">點(diǎn)此返回</a>'); } $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('投稿成功!感謝投稿!<a href="'.$current_url.'">點(diǎn)此返回</a>', '投稿成功'); } else { wp_die('投稿失??!<a href="'.$current_url.'">點(diǎn)此返回</a>'); } }
最后以UTF-8編碼保存tougao-page.php,否則中文可能會(huì)亂碼。然后進(jìn)入WordPress管理后臺(tái) – 頁(yè)面 – 創(chuàng)建頁(yè)面,標(biāo)題為投稿(可以自己起名),內(nèi)容填上投稿說(shuō)明等,右側(cè)可以選擇模板,選擇 tougao 即可。此頁(yè)面即前臺(tái)注冊(cè)頁(yè)面,將該頁(yè)面的鏈接放到網(wǎng)站任何位置,供用戶點(diǎn)擊注冊(cè)即可。
好了,基本的投稿功能已經(jīng)添加完畢,至于表單樣式不好看,表單缺少你想要的項(xiàng)目等問(wèn)題,你就自己添加css、表單項(xiàng)吧。
代碼補(bǔ)充說(shuō)明
1、如果你想讓投稿的文章立即發(fā)布,而不需要審核再編輯,那么請(qǐng)將以上代碼中的:
'post_content' => $post_content,
改成:
'post_content' => $post_content,'post_status' => 'publish',
2、如果你覺(jué)得本文提供的文章編輯框太過(guò)單調(diào),需要一個(gè)富文本編輯,你可以看看這篇文章(包含圖片上傳功能):WordPress投稿功能添加富文本編輯器
3、如果你需要投稿的文章發(fā)布后通知投稿者,可以看看這篇文章(前提投稿的文章默認(rèn)是草稿狀態(tài),而不是直接發(fā)布):WordPress投稿功能添加郵件提醒功能
7、如果你想給投稿頁(yè)面增加驗(yàn)證碼功能,可以 點(diǎn)此下載.zip 驗(yàn)證碼文件,解壓后將captcha目錄放到當(dāng)前主題目錄下,然后在代碼一中,將35行的:
<br clear="all">
改成:
<div style="text-align: left; padding-top: 10px;"> <label for="CAPTCHA">驗(yàn)證碼: <input id="CAPTCHA" style="width:110px;*float:left;" class="input" type="text" tabindex="24" size="10" value="" name="captcha_code" /> 看不清?<a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='<?php bloginfo('template_url'); ?>/captcha/captcha.php?'+Math.random();document.getElementById('CAPTCHA').focus();return false;">點(diǎn)擊更換</a> </label> </div> <div style="text-align: left; padding-top: 10px;"> <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('驗(yàn)證碼不正確!<a href="'.$current_url.'">點(diǎn)此返回</a>'); }
大功造成!