0 字
0 分钟
Typecho限制访客评论的方法
本方法用于限制访客评论功能,并在用户登录后自动返回原文页面。
注意:本教程适用于Typecho官方主题,若使用其他主题,请参考对应主题文档调整修改位置。
修改comments.php
原理是通过hasLogin()函数判断用户登录状态,将评论模块包裹在登录判断条件中。同时在登录链接中附加referer参数,实现登录后跳转回原文。
代码修改示例
<?php if($this->user->hasLogin()): ?>
//此处为评论模块(约13行开始)
<?php else: ?>
//给登录两字添加a标签。$this->permalink获取当前链接传递到登录页,以便登录后跳转回原页面。使用urlencode函数编码保证安全。
//另外一种获取当前链接的方式为$this->request->getRequestUrl(),可自行对比选择使用。
<h3><?php _e('请'); ?><a href="<?php $this->options->adminUrl('login.php'); ?>?referer=<?php echo urlencode($this->permalink); ?>"><?php _e('登录'); ?></a><?php _e('后评论'); ?></h3>
<?php endif; ?>以下为完整代码
<?php if($this->user->hasLogin()): ?>
<?php if ($this->allow('comment')): ?>
<div id="<?php $this->respondId(); ?>" class="respond">
<div class="cancel-comment-reply">
<?php $comments->cancelReply(); ?>
</div>
<h3 id="response"><?php _e('添加新评论'); ?></h3>
<form method="post" action="<?php $this->commentUrl() ?>" id="comment-form" role="form">
<?php if ($this->user->hasLogin()): ?>
<p><?php _e('登录身份: '); ?><a
href="<?php $this->options->profileUrl(); ?>"><?php $this->user->screenName(); ?></a>. <a
href="<?php $this->options->logoutUrl(); ?>" title="Logout"><?php _e('退出'); ?> »</a>
</p>
<?php else: ?>
<p>
<label for="author" class="required"><?php _e('称呼'); ?></label>
<input type="text" name="author" id="author" class="text"
value="<?php $this->remember('author'); ?>" required/>
</p>
<p>
<label
for="mail"<?php if ($this->options->commentsRequireMail): ?> class="required"<?php endif; ?>><?php _e('Email'); ?></label>
<input type="email" name="mail" id="mail" class="text"
value="<?php $this->remember('mail'); ?>"<?php if ($this->options->commentsRequireMail): ?> required<?php endif; ?> />
</p>
<p>
<label
for="url"<?php if ($this->options->commentsRequireURL): ?> class="required"<?php endif; ?>><?php _e('网站'); ?></label>
<input type="url" name="url" id="url" class="text" placeholder="<?php _e('http://'); ?>"
value="<?php $this->remember('url'); ?>"<?php if ($this->options->commentsRequireURL): ?> required<?php endif; ?> />
</p>
<?php endif; ?>
<p>
<label for="textarea" class="required"><?php _e('内容'); ?></label>
<textarea rows="8" cols="50" name="text" id="textarea" class="textarea"
required><?php $this->remember('text'); ?></textarea>
</p>
<p>
<button type="submit" class="submit"><?php _e('提交评论'); ?></button>
</p>
</form>
</div>
<?php else: ?>
<h3><?php _e('评论已关闭'); ?></h3>
<?php endif; ?>
<?php else: ?>
<h3><?php _e('请'); ?><a href="<?php $this->options->adminUrl('login.php'); ?>?referer=<?php echo urlencode($this->permalink); ?>"><?php _e('登录'); ?></a><?php _e('后评论'); ?></h3>
<?php endif; ?>同步修改sidebar.php
在 sidebar.php 中约第53行处(位于<?php else: ?>与<?php endif; ?>之间)也存在一个登录入口,建议同步添加登录后跳转逻辑。修改后示例代码如下:
<li class="last"><a href="<?php $this->options->adminUrl('login.php'); ?>?referer=<?php echo urlencode($this->request->getRequestUrl()); ?>"><?php _e('登录'); ?></a>
</li>本站未注明转载的文章均为原创,并采用
CC BY-NC-SA 4.0 授权协议,转载请注明来源,谢谢!