本文最后更新于2022年5月1日,已超过 30 天没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!
自助文章投稿,既能增加文章,访客,也能引流到自己的网站。对于很多人都比较喜欢这个功能,今天就在Sakura上实现这个功能。
开工
新建一个 tougao.php 或 名字随便,复制下面代码保存,然后上传到主题目录中。
<?php
/*
Template Name: tougao
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
while ( have_posts() ) : the_post();
get_template_part( 'tpl/content', 'page' );
endwhile; // End of the loop.
?>
<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,
});
</script>
<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>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
然后在主题文件目录的 functions.php 最底部添加下面代码(修改下面76行 邮件接收电邮地址)
//投稿
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=https://cbbkk.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('flurrychan@yahoo.com.hk', $email), "您的投稿主人已经收到啦!", $info, array('Content-Type: text/html; charset=UTF-8') );
die();
}else{
wp_die('投稿失败!');
}
}
}
(Sakura主题的不需要添加)最后再来一个CSS 适当的美化一下,保存在主题的style.css 最底部
/*投稿*/
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;}
新建面页,面页名称,模板选择 tougao 然后在菜单中显示即可。