WordPress如何实现wordpress网站投稿功能
无忧主机小编今天给大家分享的WordPress投稿功能,是在网站前台的主题总利用增加代码来实现的,就是在Wordpress网站程序文件内手动添加投稿功能代码,实现在前台提供投稿表单,用户填写相应的表格即可,用户只需要在指定的投稿页面进行填写相应内容,即可完成投稿,那么你也非常容易的获得投稿者的文章稿子;
制作投稿表单具体步骤:
1、首先在当前网站主题的目录下新建一个php文件,命名为tougao-page.php(可以随意,方便记忆即可),然后将当前主题下page.php中的所有代码复制到tougao-page.php中;
2、删除tougao-page.php开头的所有注释,即 /* 与 */ ,以及它们之间的所有注释内容;
3、将 <?php the_content(); ?> 改成以下代码:
<?php the_content(); ?>
<form method=”post” action=”<?php echo $_SERVER[“REQUEST_URI”]; ?>”>
<div style=”text-align: left; padding-top: 10px;”>
<label>昵称:*</label>
</div>
<div>
<input type=”text” size=”40″ value=”” name=”tougao_authorname” />
</div>
<div style=”text-align: left; padding-top: 10px;”>
<label>E-Mail:*</label>
</div>
<div>
<input type=”text” size=”40″ value=”” name=”tougao_authoremail” />
</div>
<div style=”text-align: left; padding-top: 10px;”>
<label>您的博客:</label>
</div>
<div>
<input type=”text” size=”40″ value=”” name=”tougao_authorblog” />
</div>
<div style=”text-align: left; padding-top: 10px;”>
<label>文章标题:*</label>
</div>
<div>
<input type=”text” size=”40″ value=”” name=”tougao_title” />
</div>
<div style=”text-align: left; padding-top: 10px;”>
<label>分类:*</label>
</div>
<div style=”text-align: left;”>
<?php wp_dropdown_categories(‘show_count=1&hierarchical=1’); ?>
</div>
<div style=”text-align: left; padding-top: 10px;”>
<label>文章内容:*</label>
</div>
<div>
<textarea rows=”15″ cols=”55″ name=”tougao_content”></textarea>
</div>
<br clear=”all”>
<div style=”text-align: center; padding-top: 10px;”>
<input type=”hidden” value=”send” name=”tougao_form” />
<input type=”submit” value=”提交” />
<input type=”reset” value=”重填” />
</div>
</form>
接着将在tougao-page.php中,把第一个 <?php 改成:
<?php
/*
* Template Name: tougao
* @author: 51php.com
* @Email : zhubinggui@foxmail.com
*/
if( isset($_POST[‘tougao_form’]) && $_POST[‘tougao_form’] == ‘send’)
{
if ( isset($_COOKIE[“tougao”]) && ( time() – $_COOKIE[“tougao”] ) < 120 )
{
wp_die(‘您投稿也太勤快了吧,先歇会儿!’);
}
// 表单变量初始化
$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) || strlen($name) > 20 )
{
wp_die(‘昵称必须填写,且长度不得超过20字’);
}
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格式’);
}
if ( empty($title) || strlen($title) > 100 )
{
wp_die(‘标题必须填写,且长度不得超过100字’);
}
if ( empty($content) || strlen($content) > 3000 || strlen($content) < 100)
{
wp_die(‘内容必须填写,且长度不得超过3000字,不得少于100字’);
}
$post_content = ‘昵称: ‘.$name.'<br />Email: ‘.$email.'<br />blog: ‘.$blog.'<br />内容:’.$content;
$tougao = array(
‘post_title’ => $title,
‘post_content’ => $post_content,
‘post_category’ => array($category)
);
// 将文章插入数据库
$status = wp_insert_post( $tougao );
if ($status != 0)
{
setcookie(“tougao”, time(), time()+180);
wp_die(‘投稿成功!感谢投稿!’);
}
else
{
wp_die(‘投稿失败!’);
}
}
代码补充说明,如果你想让让投稿的文章立即发布,而不需要审核再编辑,那么请将以上代码45行改成:
‘post_content’ => $post_content, ‘post_status’ => ‘publish’,
最后进入WordPress管理后台 》 页面 》 创建页面,标题为投稿(可以自己起名),内容填上投稿说明等,右侧可以选择模板,选择 tougao 即可好了,基本的投稿功能已经添加完毕,至于表单样式不好看,表单缺少你想要的项目等问题,你就自己添加css、表单项吧。
看见那些大博客,知名博客的投稿页面了吧,如果喜欢的话,那就赶紧也为自己的博客添加一个投稿页吧!