WordPress 自定义在线投稿页面

当你网站流量比较大的时候,你可能想到开放读者投稿的功能(如果是内容管理平台,我觉得在最初建站时就应该考虑到)。接受读者的投稿,不仅可以丰富博客的内容,还能增加与读者之间的沟通。WordPress本身并不提供投稿功能,有很多插件可以实现,但是WordPress本身拥有强大的扩展能力,所以这里我们用代码来实现投稿功能。

实现用户投稿,总体上来说有两种方法,一种是开放后台的注册功能,设置成普通用户注册进去默认身份为投稿者,登陆进去即可添加文章(文章为草稿状态);另一种方法是在前台提供投稿表单,让用户填写相应的表格即可。前一种方法实现起来相对比较简单,基本不需要管理员配置太多东西,只是有些时候你可能会觉得别扭,因为有些管理员觉得让他人看到自己的博客后台并不合适(比如企业后台一般是不标明建站程序为WordPress);而后一种方法对投稿者来说方便了很多,博主也不用担心自己网站的后台隐私,只是该方法实现起来比较麻烦,需要配置的东西很多。本文将介绍后一种方法,如果可以请多看几遍代码,希望能帮助到你。

添加投稿表单及表单数据:

1、首先在当前主题的目录下新建一个.php文件,命名为submit.php;


2、添加表单处理及代码:


<?php
/*Template Name: submit */
if( isset($_POST['submit_form']) && $_POST['submit_form'] == 'send')
{
global $wpdb;
$last_post = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC LIMIT 1");
if ( current_time('timestamp') - strtotime($last_post) < 120 )
{ wp_die(__('You submit too quickly,please wait a time!','badbye')); }
// 表单变量初始化
$name = isset( $_POST['submit_authorname'] ) ? trim(htmlspecialchars($_POST['submit_authorname'], ENT_QUOTES)) : '';
$email = isset( $_POST['submit_authoremail'] ) ? trim(htmlspecialchars($_POST['submit_authoremail'], ENT_QUOTES)) : '';
$blog = isset( $_POST['submit_authorblog'] ) ? trim(htmlspecialchars($_POST['submit_authorblog'], ENT_QUOTES)) : '';
$title = isset( $_POST['submit_title'] ) ? trim(htmlspecialchars($_POST['submit_title'], ENT_QUOTES)) : '';
$category = isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0;
$content = isset( $_POST['submit_content'] ) ? trim(htmlspecialchars($_POST['submit_content'], ENT_QUOTES)) : '';
$tags = isset( $_POST['submit_tags'] ) ? $_POST['submit_tags'] : '';
// 表单项数据验证
if ( empty($name) || mb_strlen($name) > 20 )
{
wp_die(__('Your name must be filled,and the length no exceed 20 words!','badbye'));
}

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 must be filled, and the length shall not exceed 60 characters, must comply with the Email format!','badbye'));
}

if ( empty($title) || mb_strlen($title) > 100 )
{
wp_die(__('Title must be filled, and the length should not exceed 100 words!','badbye'));
}

if ( empty($content) || mb_strlen($content) > 3000 || mb_strlen($content) < 100)
{
wp_die(__('Content must be filled out, and the length should not exceed 3000 words, not less than 100 words!','badbye'));
}

$post_content = 'Name:'.$name.'<br />Email:'.$email.'<br />Site:'.$blog.'<br />Content:'.$content;

$submit = array(
'post_title' => $title,
'post_content' => $post_content,
'tags_input' => $tags,
'post_category' => array($category)
);

// 将文章插入数据库
$status = wp_insert_post( $submit );
if ($status != 0)
{
wp_mail("664521082@qq.com","Submit","Submit_content");
wp_die(__('Success!Thank you!','badbye'));
}
else
{
wp_die(__('Submission failed!','badbye'));
}
}; ?>
<?php get_header(); ?>
<div id="container">
<?php if(have_posts()): ?><?php while(have_posts()):the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to<?php the_title_attribute(); ?>"><?php the_title();?></a></h2>
<div class="entry">
<?php the_content();?>
<?php _e('Welcome to submit!Marked with "*" are required.','badbye'); ?>
<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
<div style="text-align: left; padding-top: 10px;">
<label><?php _e('Nickname:*','badbye'); ?></label>
</div>
<div>
<input type="text" size="40" value="" name="submit_authorname" />
</div>

<div style="text-align: left; padding-top: 10px;">
<label><?php _e('E-Mail:*','badbye'); ?></label>
</div>
<div>
<input type="text" size="40" value="" name="submit_authoremail" />
</div>

<div style="text-align: left; padding-top: 10px;">
<label><?php _e('URL:','badbye'); ?></label>
</div>
<div>
<input type="text" size="40" value="" name="submit_authorblog" />
</div>

<div style="text-align: left; padding-top: 10px;">
<label><?php _e('Title:*','badbye'); ?></label>
</div>
<div>
<input type="text" size="40" value="" name="submit_title" />
</div>

<div style="text-align: left; padding-top: 10px;">
<label><?php _e('Category:*','badbye'); ?></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><?php _e('Content:*','badbye'); ?></label>
</div>
<div>
<textarea rows="15" cols="72" name="submit_content"></textarea>
</div>
<br clear="all" />
<div style="text-align: center; padding-top: 10px;">
<input type="hidden" value="send" name="submit_form" />
<div style="text-align: left;">
<label><?php _e('Tags (separate with ","):','badbye'); ?></label>
<input type="text" size="40" value="" name="submit_tags" />
</div>
<input type="submit" value="<?php _e('submit','badbye'); ?>" />
<input type="reset" value="<?php _e('reset','badbye'); ?>" />
</div>
</form>
<?php link_pages('<p><code>Pages:</strong>','</p>','number'); ?>
<?php edit_post_link(__('Edit','badbye','<p>','</p>')); ?>
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php _e('Not Found','badbye');?></h2>
<p class="entry"><?php _e('Sorry,but you are looking for something that is not here.','badbye'); ?></p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
</div>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

代码补充说明,如果想让让投稿的文章立即发布,而不需要审核再编辑,那么请将以上代码改成:'post_content' => $post_content, 'post_status' => 'publish',

然后进入WordPress管理后台——页面——创建页面,标题为投稿或你喜欢的标题,内容填上投稿说明、欢迎投稿之类的,右侧选择submit.php模板即可好了,基本的投稿功能已经添加完毕,至于表单样式或是表单项目扩展等问题,你就自己添加css样式化与慢慢琢磨吧。

没有评论:

发表评论