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: Ludou * @Email : zhouzb889@gmail测试数据 * @Blog : http://HdhCmsTestludou.org/ */ 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 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 empty ( $title ) || strlen ( $title ) > 100 ) { wp_die( '标题必须填写,且长度不得超过100字' ); } if ( empty 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、表单项吧
如何实现会员登陆后投稿?请移步到:WordPress 实现会员登陆投稿
下是实现登陆后投稿的一部分代码,我相信看到这段代码,就能实现你想要的功能了代码如下:
if (!is_user_logged_in()){ ?> <h3 class = "base-tit" >您需要登陆才能投稿!</h3> <form method= "post" action= "<?php bloginfo('url'); ?>/wp-login.php" name= "loginform" class = "submit-signin" > <ul> <li><label><b>用户名:</b><input type= "text" size= "20" value= "" name= "log" class = "ipt" ></label></li> <li><label><b>密码:</b><input type= "password" size= "20" value= "" name= "pwd" class = "ipt" ></label></li> <li><input type= "submit" value= "立即登录" name= "submit" class = "btn btn-primary" ></li> <li><a href= "<?php bloginfo('url'); ?>/wp-login.php?action=register" class = "btn btn-mini" >注册</a><a href= "<?php bloginfo('url'); ?>/wp-login.php?action=lostpassword" class = "btn btn-mini" >找回密码</a></li> </ul> <input type= "hidden" value= "<?php bloginfo('url'); ?>/submit" name= "redirect_to" > </form> <?php } else {?> <h3 class = "base-tit" >开始投稿!</h3> 这里是投稿的表单,可自行设计........ <?php }?>如想要跟大前端效果一样,那表单处添加这样的HTML代码:
< h3 class = "base-tit" > 您需要登陆才能投稿! </ h3 > < form method = "post" action = "<?php bloginfo('url'); ?>/wp-login.php" name = "loginform" class = "submit-signin" > < ul > < li > < label > < b > 用户名: </ b > < input type = "text" size = "20" value = "" name = "log" class = "ipt" > </ label > </ li > < li > < label > < b > 密码: </ b > < input type = "password" size = "20" value = "" name = "pwd" class = "ipt" > </ label > </ li > < li > < input type = "submit" value = "立即登录" name = "submit" class = "btn btn-primary" > </ li > < li > < a href = "<?php bloginfo('url'); ?>/wp-login.php?action=register" class = "btn btn-mini" > 注册 </ a > < a href = "<?php bloginfo('url'); ?>/wp-login.php?action=lostpassword" class = "btn btn-mini" > 找回密码 </ a > </ li > </ ul > < input type = "hidden" value = "<?php bloginfo('url'); ?>/submit" name = "redirect_to" > </ form > <? php }else{ ?> < h3 class = "base-tit" > 开始投稿! </ h3 > < ul class = "submit-form" > < li > < label > < span class = "submit-tit" > < em > * </ em > 文章标题 </ span > < input type = "text" class = "ipt ipt-submit-title u_ipt_error" placeholder = "写点什么..." size = "40" > < span class = "u_tip" style = "display: inline-block;" > 标题不能为空! </ span > </ label > </ li > < li > < label > < span class = "submit-tit" > < em > * </ em > 所属分类 </ span > <? php wp_dropdown_categories(' show_count = 1 & hierarchical = 1 & exclude = 1 ,97,149,461& class = ipt -submit-cat'); ?> </ label > </ li > < li > < label > < span class = "submit-tit" > < em > * </ em > 文章网址 </ span > < input type = "url" class = "ipt ipt-submit-url" size = "40" value = "http://" > < span class = "u_tip" > </ span > </ label > </ li > < li > < label > < span class = "submit-tit" > < em > * </ em > 作者 </ span > < input type = "url" class = "ipt ipt-submit-author" size = "40" value = "<?php echo $u_name;?>" > < span class = "u_tip" > </ span > </ label > </ li > < li > < label > < span class = "submit-tit" > < em > * </ em > E-mail </ span > < input type = "url" class = "ipt ipt-submit-email" size = "40" value = "<?php echo $u_mail;?>" > < span class = "u_tip" > </ span > </ label > </ li > < li > < label > < span class = "submit-tit" > < em > * </ em > 文章内容 </ span > < textarea placeholder = "写点什么..." class = "submit-content u_ipt_error" > </ textarea > < span class = "u_tip u_tip_content" style = "display: inline-block;" > 内容不能为空! </ span > </ label > </ li > < li > < input type = "button" value = "立即提交" class = "btn btn-primary dosubmit" > < input class = "btn" type = "reset" value = " 重 填 " /> </ li > < li style = "padding-top:10px;" > < span class = "tip" > </ span > </ li > </ ul >接着就是CSS样式了,代码如下:
<style> .submit-form li{margin-bottom: 12px;padding-left: 100px;clear: both;position: relative;} .submit-tit{width: 94px;float: left;margin-left: -100px;text-align: right;font-size: 14px;line-height: 26px;} .submit-form label em{color: #f00;font-style: normal;font-family: fantasy;margin-right: 2px;} .submit-form .ipt{width: 300px;} .submit-form select{border: 1px solid #D2D2D2;width: 314px;padding: 2px;border-radius: 2px;height: 30px;} .submit-form textarea{border: 1px solid #D2D2D2;width: 98%;padding: 5px;border-radius: 2px;height: 250px;} .submit-form .btn-primary{width: 90px;margin-right: 10px;} .u_tip{position: absolute;margin-left: 10px;background-color: #F2DEDE;border: 1px solid #EED3D7;border-radius: 2px;height: 29px;line-height: 29px;padding: 0 10px;color: #B94A48;display: none;} .u_ipt_error,.submit-form textarea.u_ipt_error{border: 1px solid #E0BAD2;box-shadow: 0 0 4px #E0BAD2;} .u_tip_content{bottom: -42px;left: 47%} .tip{background-color: #D9EDF7;border: 1px solid #BCE8F1;border-radius: 2px;padding: 6px 10px;color: #3A87AD;font-weight: bold;display: none;} .article-entry ul{margin-left: 36px;} .submit-signin ul{padding:10px 0 0 200px;} .submit-signin li{clear:both;margin-bottom: 15px;} .submit-signin b{float: left; width: 195px;text-align: right; margin-left: -200px;line-height: 29px;font-size: 14px;} .submit-signin .ipt{width: 220px;font-weight: bold} .submit-signin .btn-primary{width: 100px;} .submit-signin .btn-mini{margin-right: 10px;} </style>对了,还有表单处需要一些参数,以下代码是调用参数的,代码如下:
<?php global $current_user ; get_currentuserinfo(); $cur_id = $current_user ->ID; $user_info = get_userdata( $cur_id ); $u_login = $user_info ->user_login; $u_mail = $user_info ->user_email; $u_time = $user_info ->user_registered; $u_name = get_user_meta( $cur_id , 'nickname' ,true); ?>好了,应该差不多了,我以经毫无保留的把代码贴出来了,能不能实现就要看你的造化了,至于如何提交,那是本站的核心代码,这个本站恕不分享.
查看更多关于WordPress实现投稿与会员登录投稿功能 - WordPress的详细内容...