免外掛打造wordpress超完美投稿頁面
阿新 • • 發佈:2020-09-18
一、新建投稿頁面模板
把主題的 page.php 另存為 tougao.php,並且在第一行的
1 2 3 |
/*
Template Name: tougao
*/
|
把下面的程式碼甩進functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
//投稿
if ( isset( $_POST [ 'tougao_form' ]) && $_POST [ 'tougao_form' ] == 'send' ){
if ( isset( $_COOKIE [ "tougao" ]) && ( time() - $_COOKIE [ "tougao" ] ) < 120 ){
wp_die( '您投稿也太勤快了吧,先歇會兒,2分鐘後再來投稿吧!' );
}
// 表單變數初始化
$name = trim( $_POST [ 'tougao_authorname' ]);
$email = trim( $_POST [ 'tougao_authoremail' ]);
$site = trim( $_POST [ 'tougao_site' ]);
$title = strip_tags (trim( $_POST [ 'tougao_title' ]));
$category = isset( $_POST [ 'cat' ] ) ? (int) $_POST [ 'cat' ] : 0;
$content = $_POST [ 'tougao_content' ];
$tags = strip_tags (trim( $_POST [ 'tougao_tags' ]));
if (! empty ( $site )){
$author = '<a href="' . $site . '" title="' . $name . '" target="_blank" rel="nofollow">' . $name . '</a>' ;
} else {
$author = $name ;
}
$info = '感謝: ' . $author . ' ' . '投稿' . ' 。' ;
global $wpdb ;
$db = "SELECT post_title FROM $wpdb->posts WHERE post_title = '$title' LIMIT 1" ;
if ( $wpdb ->get_var( $db )){
wp_die( '發現重複文章.你已經發表過了.或者存在該文章' );
}
// 表單項資料驗證
if ( $name == '' ){
wp_die( '暱稱必須填寫,且長度不得超過20個字' );
} elseif (mb_strlen( $name , 'UTF-8' ) > 20 ){
wp_die( '你的名字怎麼這麼長啊,起個簡單易記的吧,長度不要超過20個字喲!' );
} elseif ( $title == '' ){
wp_die( '文章標題必須填寫,長度6到50個字之間' );
} elseif (mb_strlen( $title , 'UTF-8' ) > 50 ){
wp_die( '文章標題太長了,長度不得超過50個字' );
} elseif (mb_strlen( $title , 'UTF-8' ) < 6 ){
wp_die( '文章標題太短了,長度不得少於過6個字' );
} elseif ( $email == '' || !preg_match( "/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix" , $email )){
wp_die( 'Email必須填寫,必須符合Email格式' );
} elseif ( $content == '' ){
wp_die( '內容必須填寫,不要太長也不要太短,300到10000個字之間' );
} elseif (mb_strlen( $content , 'UTF-8' ) >10000){
wp_die( '你也太能寫了吧,寫這麼多,別人看著也累呀,300到10000個字之間' );
} elseif (mb_strlen( $content , 'UTF-8' ) < 300){
wp_die( '太簡單了吧,才寫這麼點,再加點內容吧,300到10000個字之間' );
} elseif ( $tags == '' ){
wp_die( '不要這麼懶嗎,加個標籤好人別人搜到你的文章,長度在2到20個字!' );
} elseif (mb_strlen( $tags , 'UTF-8' ) < 2){
wp_die( '不要這麼懶嗎,加個標籤好人別人搜到你的文章,長度在2到20個字!' );
} elseif (mb_strlen( $tags , 'UTF-8' ) > 40){
wp_die( '標籤不用太長,長度在2到40個字就可以了!' );
} elseif ( $site == '' ){
wp_die( '請留下貴站名稱,要不怎麼宣傳呀,這點很重要哦!' );
} elseif ( $site == '' ){
wp_die( '請填寫原文連結,好讓其他人瀏覽你的網站,這是最重要的宣傳方式哦!' );
} else {
$post_content = $info . '<br />' . $content ;
$tougao = array (
'post_title' => $title ,
'post_content' => $post_content ,
'tags_input' => $tags ,
'post_status' => 'pending' , //publish
'post_category' => array ( $category )
);
// 將文章插入資料庫
$status = wp_insert_post( $tougao );
if ( $status != 0){
setcookie( "tougao" , time(), time()+1);
echo ( '<div style="text-align:center;">' . '<title>' . '你好,劉!' . '</title>' . '</div>' );
echo ( '<div style="text-align:center;">' . '<meta charset="UTF-8" /><meta http-equiv="refresh" content="5;URL=http://www.hilau.com">' . '</div>' );
echo ( '<div style="position:relative;font-size:14px;margin-top:100px;text-align:center;">' . '投稿成功,感謝投稿,5秒鐘後將返回網站首頁!' . '</div>' );
echo ( '<div style="position:relative;font-size:20px;margin-top:30px;text-align:center;">' . '<a href="/" >' . '立即返回網站首頁' . '</a>' . '</div>' );
wp_mail( array ( '[email protected]' , $email ), "您的投稿主人已經收到啦!" , $info , array ( 'Content-Type: text/html; charset=UTF-8' ) );
die ();
} else {
wp_die( '投稿失敗!' );
}
}
}
|
上面用到了wordpress的自定義域功能(已經註釋掉了)如果需要可以自行開啟,這樣就可以方便設定投稿人暱稱和投稿人網址。
二.接著找到tougao.php檔案中的the_content();函式,在其後插入下面的程式碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
< form method = "post" action = "<?php echo $_SERVER[" REQUEST_URI"]; $ current_user = wp_get_current_user (); ?>" class="row">
< div class = "t1 tt col-sm-4" >
< span >
< input class = "form-control" type = "text" size = "40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_login; ?>" name="tougao_authorname"; id="tougao_authorname" tabindex="1" />
</ span >
< span >
< label >您的暱稱(不超20字)</ label >
</ span >
</ div >
< div class = "t2 tt col-sm-4" >
< span >
< input class = "form-control" type = "text" size = "40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_email; ?>" name="tougao_authoremail" id="tougao_authoremail" tabindex="2" />
</ span >
< span >
< label >您的郵箱</ label >
</ span >
</ div >
< div class = "t3 tt col-sm-4" >
< span >
< input class = "form-control" type = "text" size = "40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_url; ?>" name="tougao_site" id="tougao_site" tabindex="4" />
</ span >
< span >
< label >貴站網址</ label >
</ span >
</ div >
< div class = "t4 tt col-sm-4" >
< span >
< input class = "form-control" type = "text" size = "40" value = "" name = "tougao_title" id = "tougao_title" tabindex = "3" />
</ span >
< span >
< label >文章標題(6到50字之間)</ label >
</ span >
</ div >
< div class = "t5 tt col-sm-4" >
< span >
< input class = "form-control" type = "text" size = "40" value = "" name = "tougao_tags" id = "tougao_tags" tabindex = "5" />
</ span >
< span >
< label >文章標籤(2到20字之間並以英文逗號分開)</ label >
</ span >
</ div >
< div class = "t6 tt col-sm-4" >
< span >
<? php wp_dropdown_categories(' show_count = 0 & hierarchical = 1 & hide_empty = 0 '); ?>
</ span >
< span >
< label >文章分類*(必選)</ label >
</ span >
</ div >
< div class = "clear" ></ div >
< div id = "postform" >
< textarea rows = "15" cols = "70" class = "form-control col-sm-12" id = "tougao_content" name = "tougao_content" tabindex = "6" /></ textarea >
< p >字數限制:300到10000字之間</ p >
</ div >
< div class = "col-sm-12" id = "submit_post" >
< input type = "hidden" value = "send" name = "tougao_form" />
< input class = "btn btn-danger" type = "submit" name = "submit" value = "發表文章" tabindex = "7" />
< input class = "btn btn-outline-secondary" type = "reset" name = "reset" value = "重填內容" tabindex = "8" />
</ div >
</ form >
|
如果需要在文字框上加上tinymce編輯器,可以在該檔案里加上:
1 2 3 4 5 6 7 8 |
<script type= "text/javascript" src= "<?php echo home_url(); ?>/wp-includes/js/tinymce/tinymce.min.js" ></script>
<script type= "text/javascript" >
tinymce.init({
selector : '#tougao_content' ,
menubar: false ,
//toolbar: false,
});
</script>
|
上面的css樣式可以參考這個:小旋風·泛目錄站群V5.4/無限制版本
1 2 3 4 5 6 |
form{ padding : 0 20px ;}
form div.tt{ float : left ; width : 460px ; margin : 10px 0 ;}
form input,.t 6 #cat{ width : 250px ; height : 30px ; padding : 0 10px ;}
#submit_post{ margin : 20px 0 ;}
#submit_post input{ width : 150px ; height : 50px ;}
#postform{ width : 100% ; padding : 0 15px ;}
|
大功告成!