WordPress用自带Tinymce制作投稿功能页面(无需插件)

网站APP, 转自点点 13 年前 回复

, , , ,

第一步:在你目前使用的模板的根目录创建一个php文件,命名为tougao.php,然后把下面的代码复制到tougao.php里面:

/**Template Name: Tougao*/
<?php get_header(); ?>
<?php
/************TinyMCE3模式,也是WordPress老版本**********************
<script type="text/javascript" src="<?php echo home_url(); ?>/wp-includes/js/tinymce/tinymce.js"></script>
<script type="text/javascript">
	tinyMCE.init({
	// General options
	mode : "exact",
	theme : "advanced",
	elements: "tougao_content",
	skin : "o2k7",
	// Theme options
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,forecolor,outdent,link,unlink,|,bullist,numlist,indent,|,justifyleft,justifycenter,justifyright,justifyfull,image,code",
	theme_advanced_buttons2 : false,
	theme_advanced_buttons3 : false,
	theme_advanced_buttons4 : false,
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "none",
	theme_advanced_resizing : true,
	});
</script>
******************TinyMCE3模式,也是WordPress老版本************************/
?>
/***TinyMCE4模式,也是现在WordPress4.9+***/
<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',
	plugins: 'image link media hr lists',
	menubar: false
});
</script>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div id="main-column">
	<h1 style="font-size:14px;font-weight:bold;"class="header-division topbar-spacer"><a href="<?php the_permalink(); ?>" rel="bookmark" target="_blank" ><?php the_title(); ?></a></h1>
	<?php the_content(); ?>
	<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; $current_user = wp_get_current_user(); ?>">
	<div>
		<span>
			<input 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>您的昵称*(必填)</label>
		</span>
	</div>
	<div>
		<span>
			<input 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>
		<span>
			<input 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>
		<span>
			<input type="text" size="40" value="" name="tougao_title" id="tougao_title" tabindex="3" />
		</span>
		<span>
			<label>文章标题*(必填)</label>
		</span>
	</div>
	<div>
		<span>
			<input type="text" size="40" value="" name="tougao_tags" id="tougao_tags" tabindex="5" />
		</span>
		<span>
			<label>文章标签*(必填)</label>
		</span>
	</div>
	<div>
		<span>
			<?php wp_dropdown_categories('show_count=1&hierarchical=1&exclude=349'); ?>
		</span>
		<span>
			<label>文章分类*(必选)</label>
		</span>
	</div>
	<div id="postform">
		<textarea rows="15" cols="60" id="tougao_content" name="tougao_content" tabindex="6" ></textarea>
	</div>
	<div id="submit_post">
		<input type="hidden" value="send" name="tougao_form" />
		<input type="submit" name="submit" value="发表文章" tabindex="7" />
		<input type="reset" name="reset" value="重填内容" tabindex="8" />
	</div>
	</form>
</div>
	<?php endwhile ?>
	<?php endif ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

第二步:在目前的模板下的function.php添加如下判断语句代码:

<?php
//投稿
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)){
		if(substr($site, 0, 7) != 'http://') $site= 'http://'.$site;
		$author='<a href="'.$site.'" title="'.$name.'" target="_blank" rel="nofollow">'.$site.'</a>';
	}else{
		$author=$site;
	}
	$info='感谢: '.$name.' '.'投稿'.' ';
	$post_link='文章来源: '.$author."\n\n";
	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.$post_link.'<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>'.'WordPress UED'.'</title>'.'</div>');
		echo ('<div style="text-align:center;">'.'<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>');
		die();
	}else{
		wp_die('投稿失败!');
	}
	}
}
?>

第三步:添加一个新页面并选择Tougao页面模板即可。

支付宝打赏微信打赏

如果此文对你有帮助,欢迎打赏作者。

发表评论

欢迎回来 (打开)

(必填)