现在很多个人博客为了增加博客的内容,都会提供投稿通道,大部分都是以邮箱的形式进行投稿,不过这样一来,也很费人力,要拷贝复制,然后编辑等。如果给博客加个在线投稿功能,那就方便多了。稍微审核下文章内容就可以了。
不用插件用代码解决wordpress网站在线投稿功能
经过几种尝试,在露兜博客找到了合适的方法,并经过微调。勉强可用了,该文章针对的是本站之前发布的好看的erphpdown个人中心文章为基础
在erphpdown文件夹下建立一个php文件,命名为submission-form之类的
在上述文件里添加表单样式代码
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == 'send'){
if( isset($_COOKIE["tougao"]) && ( time() - $_COOKIE["tougao"] ) < 100 ){
echo ('您投稿也太勤快了吧,先歇会儿! <a href="javascript:void(0);" onclick="history.back();">点此返回</a>');die() ;
}
//表单变量初始化
$name = isset( $_POST['tougao_authorname'] ) ? $_POST['tougao_authorname'] : '';
$email = isset( $_POST['tougao_authoremail'] ) ? $_POST['tougao_authoremail'] : '';
$title = isset( $_POST['tougao_title'] ) ? $_POST['tougao_title'] : '';
$tags = isset( $_POST['tougao_tags'] ) ? $_POST['tougao_tags'] : '';
$category = isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0;
$content = isset( $_POST['tou-content'] ) ? $_POST['tou-content'] : '';
//表单项数据验证
if ( empty($email) || strlen($email) > 60 || !preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)){
echo ('邮箱必须填写,且不得超过60个字符,必须符合 Email 格式 <a href="javascript:void(0);" onclick="history.back();">点此返回</a>');die() ;
}
if ( empty($title) || strlen($title) > 100 ){
echo ('文章标题必须填写,且不得超过100个字符 <a href="javascript:void(0);" onclick="history.back();">点此返回</a>');die() ;
}
if ( empty($content) || strlen($content) < 100){
echo ('内容必须填写,且不得少于100个字符 <a href="javascript:void(0);" onclick="history.back();">点此返回</a>');die() ;
}
$tougao = array('post_title' => $title,'post_content' => $content,'post_status' => 'pending','tags_input' => $tags,'post_category' => array($category));
$status = wp_insert_post( $tougao );//将文章插入数据库
if ($status != 0){
global $wpdb;
$myposts = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_status = 'pending' AND post_type = 'post' ORDER BY post_date DESC");
add_post_meta($myposts[0]->ID, 'postauthor', $name);
if( !empty($blog))
add_post_meta($myposts[0]->ID, 'authorurl', $blog);
setcookie("tougao", time(), time()+180);
echo ('投稿成功!<a href="javascript:void(0);" onclick="history.back();">点此返回</a>');die() ;
}else{
echo ('投稿失败!<a href="javascript:void(0);" onclick="history.back();">点此返回</a>');die() ;
}
}
?>
<style type="text/css">
#tou-primary {
width: 100%;
}
#tou-basicinfo p {
text-indent: 0;
}
.postform {
background: #fff;
width: 40%;
margin: 5px 0;
padding: 5px;
border: 1px solid #ebebeb;
border-radius: 2px;
-webkit-appearance: none;
}
.post-area {
margin-top: 10px;
}
#tou-basicinfo label {
float: left;
width: 80px;
line-height: 40px;
}
#tou-basicinfo input {
background: #fff;
width: 40%;
margin: 5px 0;
padding: 5px;
border: 1px solid #ebebeb;
border-radius: 2px;
-webkit-appearance: none;
}
#tou-basicinfo input:focus {
outline: 0;
border: 1px solid #568abc;
}
textarea, #tou-content{
background: #fff;
width: 100%;
margin: 5px 0;
padding: 5px;
border: 1px solid #ebebeb;
border-radius: 2px;
-webkit-appearance: none;
}
#submit {
background: #0088cc;
float: left;
width: 40%;
height: 40px;
color: #fff;
text-align: center;
margin: 10px 0 0 0;
border: 0px;
cursor: pointer;
border-radius: 2px;
-webkit-appearance: none;
box-shadow: 0 0 2px rgba(0, 0, 0, 0.4);
}
#submit:hover {
background: #a3a3a3;
}
.mce-path-item {
display: none;
}
.mce-stack-layout{
border: 1px solid #ebebeb;
}
#sc_select {
display: none;
}
</style>
<div id="tou-primary">
<main id="tou-main">
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="tou-content">
<div class="single-content">
<?php the_content(); ?>
<?php if ( current_user_can('level_0') ){ ?>
<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
<div id="tou-basicinfo">
<p>
<label>E-Mail:</label>
<input type="text" value="" name="tougao_authoremail" placeholder="必填" required/>
</p>
<p>
<label>文章标题:</label>
<input type="text" value="" name="tougao_title" placeholder="必填" required/>
</p>
<p>
<label>选择分类:</label>
<?php wp_dropdown_categories('show_count=0&hierarchical=1'); ?>
</p>
<p>
<label>关键字:</label>
<input type="text" value="" name="tougao_tags" placeholder="选填"/>
</p>
</div>
<div>
<label>文章内容(不少于100个字):</label>
<div class="post-area">
<?php
$post = false;
$content = '';
$editor_id = 'tou-content';
$settings = array(
'textarea_rows' => 10
);
wp_editor( $content, $editor_id, $settings );
?>
</div>
<p>
<input type="hidden" value="send" name="tougao_form" />
<input id="submit" name="submit" type="submit" value="提交文章" />
</p>
</form>
<?php } ?>
<div class="clear"></div>
</div>
</div><!-- .tou-content -->
</article><!-- #page -->
<?php endwhile; ?>
</main><!-- .site-main -->
</div><!-- .content-area -->
3.修改样式
/*我要投稿*/
.wp-switch-editor{
border-radius: 2px!important;
}
#tou-primary input[type=button], #tou-primary button {
border-radius: 0!important;
}
.wp-core-ui .button, .wp-core-ui .button-secondary {
background: #f3f5f6!important;
}
4.修改个人中心模板文件,在导航添加导航菜单
<li <?php if($_GET["pd"]=='tougao'){?>class="active"<?php }?> ><a href="?pd=tougao" ><i class="er er-comment"></i> 我要投稿</a></li>
5.添加内容也在合适位置引入第一步文件
<?php }elseif($_GET["pd"]=='tougao'){////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////我要投稿
?>
<div>
<?php get_template_part( 'css/erphpdown/submission-form' ); ?>
</div>
6.不出意外的话,就OK了,当然你可以逐步调整代码,进一步完善。
注意文件引用路径,布局问题自行修改css代码;
该代码已知问题:提交后直接刷新网页会重复提交文章请注意,正在解决!
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏