共計 4166 個字符,預計需要花費 11 分鐘才能閱讀完成。
這篇文章主要講解了“WordPress 中如何添加投稿功能”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“WordPress 中如何添加投稿功能”吧!
一、添加投稿表單
1、首先在當前主題的目錄下新建一個 php 文件,命名為 tougao-page.php,然后將 page.php 中的所有代碼復制到 tougao-page.php 中;
2、刪除 tougao-page.php 開頭的所有注釋,即 /* 與 */,以及它們之間的所有內容;
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(); ?
!-- 關于表單樣式,請自行調整 --
form >二、添加表單處理代碼 在 tougao-page.php 開頭處中,將第一個 ?php 改成代碼二:
?php
* Template Name: tougao
* 作者:露兜
* 博客:https://www.ludou.org/
*
* 更新記錄
* 2010 年 09 月 09 日 : * 首個版本發布
*
* 2011 年 03 月 17 日 : * 修正時間戳函數,使用 wp 函數 current_time(timestamp)替代 time()
*
* 2011 年 04 月 12 日 : * 修改了 wp_die 函數調用,使用合適的頁面 title
*
* 2013 年 01 月 30 日 : * 錯誤提示,增加點此返回鏈接
*
* 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:// 你的投稿頁面地址 // 注意修改此處的鏈接地址
$last_post = $wpdb- get_var( SELECT `post_date` FROM `$wpdb- posts` ORDER BY `post_date` DESC LIMIT 1
// 博客當前最新文章發布時間與要投稿的文章至少間隔 120 秒。 // 可自行修改時間間隔,修改下面代碼中的 120 即可
// 相比 Cookie 來驗證兩次投稿的時間差,讀數據庫的方式更加安全
if ( (date_i18n( U) - strtotime($last_post)) 120 ) {
wp_die( 您投稿也太勤快了吧,先歇會兒!a href= .$current_url. 點此返回 /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)) :
// 表單項數據驗證
if ( empty($name) || mb_strlen($name) 20 ) {
wp_die( 昵稱必須填寫,且長度不得超過 20 字。a href= .$current_url. 點此返回 /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 必須填寫,且長度不得超過 60 字,必須符合 Email 格式。a href= .$current_url. 點此返回 /a
}
if ( empty($title) || mb_strlen($title) 100 ) {
wp_die( 標題必須填寫,且長度不得超過 100 字。a href= .$current_url. 點此返回 /a
}
if ( empty($content) || mb_strlen($content) 3000 || mb_strlen($content) 100) {
wp_die( 內容必須填寫,且長度不得超過 3000 字,不得少于 100 字。a href= .$current_url. 點此返回 /a
}
$post_content = 昵稱: .$name. br / Email: .$email. br / blog: .$blog. br / 內容: br / .$content;
$tougao = array(
post_title = $title,
post_content = $post_content,
post_category = array($category)
);
// 將文章插入數據庫
$status = wp_insert_post( $tougao );
if ($status != 0) {
// 投稿成功給博主發送郵件
// somebody#example.com 替換博主郵箱
// My subject 替換為郵件標題,content 替換為郵件內容
wp_mail( somebody#example.com , My subject , content
wp_die( 投稿成功!感謝投稿!a href= .$current_url. 點此返回 /a , 投稿成功
}
else {
wp_die( 投稿失敗!a href= .$current_url. 點此返回 /a
}
}
最后以 UTF- 8 編碼保存 tougao-page.php,否則中文可能會亂碼。然后進入 WordPress 管理后臺 – 頁面 – 創建頁面,標題為投稿(可以自己起名),內容填上投稿說明等,右側可以選擇模板,選擇 tougao 即可。此頁面即前臺注冊頁面,將該頁面的鏈接放到網站任何位置,供用戶點擊注冊即可。
好了,基本的投稿功能已經添加完畢,至于表單樣式不好看,表單缺少你想要的項目等問題,你就自己添加 css、表單項吧。最后,也歡迎給本站投稿哦,當然本站的投稿方式是開放后臺的注冊功能,不是以上的表單形式。
代碼補充說明
1、如果你想讓投稿的文章立即發布,而不需要審核再編輯,那么請將以上代碼中的:
post_content = $post_content,
改成:
post_content = $post_content, post_status = publish ,
2、如果你想給投稿頁面增加驗證碼功能,可以 點此下載 驗證碼文件,解壓后將 captcha 目錄放到當前主題目錄下,然后在代碼一中,將 35 行的:
br clear= all
改成:
div > label for= CAPTCHA 驗證碼:
input id= CAPTCHA > /label
/div
div > 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( 驗證碼不正確!a href= .$current_url. 點此返回 /a
}
感謝各位的閱讀,以上就是“WordPress 中如何添加投稿功能”的內容了,經過本文的學習后,相信大家對 WordPress 中如何添加投稿功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關知識點的文章,歡迎關注!