原来是访客评论之后记录cookies作为登录者需要退出时的一个退出按钮,现在替换成【换个马甲】功能,点击它会弹出评论者昵称、邮箱、网址的表单让访客重新填写另一个身份来评论。
遇到的主要问题
主要在于评论者身份的表单value值是否已经填写,如果是空白的,那么即便显示登录了,也没法提交,因为表单不能为空,其中三个表单的value值必须与当前登录身份一致,所以必须用一个函数来输出各种信息。
添加到functions.php的自定义函数
// 获取当前用户 function inlojv_get_current_user() { global $current_user; if (! is_user_logged_in ()) { $user = wp_get_current_commenter (); if (! empty ( $user ['comment_author'] )) { // PHP5.4以上因为对象还没创建会报错,所以声明个数组再强制转为对象 $current_user = array (); $current_user = ( object ) $current_user; $current_user->display_name = $user ['comment_author']; $current_user->user_email = $user ['comment_author_email']; $current_user->user_url = $user ['comment_author_url']; } else { return false; } } return $current_user; }
Comments.php内的判断
判断当用户登录时显示用户身份,否则显示未登录需要填写的表单,下面引用Patience主题的comments.php模板来看如何使用上面定义的inlojv_get_current_user()
函数来判断与输出表单的value值
<form method="post" action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" id="comment_form"> <div class="r-submit2"> <?php $current_user = inlojv_get_current_user(); ?> <?php if( $current_user !== false ) { ?> <p>登录身份 <?php echo $current_user->display_name; ?> <a href="javascript:ChangeId();" id="changeid" title="置换ID">[ 换个马甲 ]</a></p> <?php } ?> <div class="comment-input<?php echo $current_user !== false ? ' hide' : null;?>"> <p> <label for="author" class="required"><?php _e('昵称'); ?></label> <input type="text" name="author" id="author" class="text" placeholder="name" value="<?php echo !empty ( $current_user->display_name ) ? $current_user->display_name : '昵称'; ?>" required/> </p> <p> <label for="mail" class="required"><?php _e('邮箱'); ?></label> <input type="email" name="email" id="mail" class="text" placeholder="name@example.com" value="<?php echo !empty ( $current_user->user_email ) ? $current_user->user_email : '邮箱'; ?>" required/> </p> <p> <label for="url"><?php _e('网站'); ?></label> <input type="url" name="url" id="url" class="text" placeholder="http://example.com/" value="<?php echo !empty ( $current_user->user_url ) ? $current_user->user_url : '网址'; ?>" /> </p> </div> </div> <div class="r-submit1"> <div class="comt_loading"><div></div></div> <textarea rows="8" cols="50" name="comment" class="textarea" id="c_tarea"></textarea> <div class="comt-bq"> <a class="comt-addsmilies" href="javascript:;"><i class="fa fa-magnet"></i> 表情</a> <div class="comt-smilies"><?php inlo_smilies() ?></div> <div class="clear"></div> </div> <button type="submit" name="submit" class="submit" id="submit"><?php _e('提交评论'); ?></button> </div> <div class="clear"></div> <?php comment_id_fields(); ?> <?php do_action('comment_form', $post->ID); ?> </form>
JS控制点击[换个马甲]按钮
看上面模板的换个马甲a标签,href值为触发js控制的函数,所以我们在js中定义这个函数以及相关动作:
//———————————————————————————————————————————————— // 换马甲 //———————————————————————————————————————————————— function ChangeId() { $('.comment-input').slideToggle('normal', function(){ if ( $('.comment-input').css('display') == 'none' ) { $('#changeid').text("[ 换个马甲 ]"); } else { $('#changeid').text("[ 暂时不换 ]"); } }); }
注:Ajax下不需要重载这个ChangeId()函数。That's All . Cheers !
2015年3月10日更新
后来发现,虽然点击【换个马甲】能滑出评论表单,但是改了评论昵称去提交评论 发现昵称并没有变更,所以还少一个判断。
<?php if ( ! $user_ID ): // 若不是管理员则显示评论表单 ?>...<?php endif; ?>
要用这一句判断来包住评论表单(昵称、邮箱、网址的div部分)
,包住之后就解决了。
不过这样一来,管理员就无法切换身份了,因为上面是非管理员才会滑出表单,所以可以再在管理员身份时加一个判断,管理员改为【退出登录】,非管理员则为【换个马甲】即可。
本站文章除注明转载/出处外,均为本站原创或翻译。若要转载但请务必注明出处,尊重他人劳动成果。
转载请注明出处链接 : https://www.inlojv.com/4598.html