免插件打造wordpress超完美投稿页面

一、新建投稿页面模板
把主题的 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('i@hjyl.org', $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,.t6 #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;}

大功告成!

posted @ 2020-09-18 22:11  小军递  阅读(844)  评论(0)    收藏  举报