用代码武装你的wordpress [part 2]

» 2009-10-12 64条评论

快速链接:用代码武装你的wordpress [part 1]

写完 part 1 后脑子一片空白,因为 part 2 还没想好怎么写(应该说怎么贴),哈哈。

今天脑子清醒些,那么继续吧

七、Ctrl + Enter 提交评论

这个功能实现代码我是在木木木木木那里搬来的,具体出在哪里就去木木木木木那搜文章看吧,哈。方法:

打开所使用的主题文件的 comments.php,找到类似如下代码:

<textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"};"></textarea>

"<textarea>"内加上下面红色部分代码:

<textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4" onkeydown="if(event.ctrlKey&&event.keyCode==13){document.getElementById('submit').click();return false};"></textarea>

OK,搞定,简单吧,哈。

2010.7.2 更新:更换&&为html代码,通过W3C验证

<textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4" onkeydown="if(event.ctrlKey&amp;&amp;event.keyCode==13){document.getElementById('submit').click();return false};"></textarea>

八、让不同页面显示不同的文章数量

同样代码是从木木木木木那里搬来的,代码很简单,下面举个例说明一下:

例如你要在分类页面显示30条文章,那么打开主题文件 archive.php,找到下面这句:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

在其上面加入如下代码:

<?php $posts = query_posts($query_string . '&orderby=date&showposts=30'); ?><!--控制文章显示数量,30即为文章数量-->

这个功能有人做成插件,既然是一句话就能解决的何必要用插件呢?(不过我用过这插件《加了个WP插件:Different Posts Per Page》)哈

九、防 Spam 代码:WP Anti Spam 小牆 1.7

最新版移步至
WP Anti Spam 小牆 1.81》-1.8的修改版
WP Anti Spam 小牆 1.8

一小段代码实现防 Spam 功能,厉害吧,又省了一个插件了。这段代码出自 Willin 大师之手,以后有什么更新请关注 Willin
的原文章:《WP Anti Spam 小牆 1.7》。下面是方法:

将下面代码 copy 到 Function.php 最后

<?php

/* -----------------------------------------------

<<小牆>> Anti-Spam v1.7 by Willin Kan.

*/

//建立

class anti_spam {

function anti_spam() {

if (!current_user_can('level_0')) {

add_action('template_redirect',array($this,'w_tb'),1);

add_action('init',array($this,'gate'),1);

add_action('preprocess_comment',array($this,'sink'),1); } }

//設欄位

function w_tb() {

if (is_singular()) {

ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=(["'])comment(["'])(.+)</textarea>#","textarea$1name=$2w$3$4</textarea>

<textarea name="comment" cols="100%" rows="8" style="position:absolute;top:-500px;"></textarea>",$input);'));

} }

//檢查

function gate() {

if (isset($_POST['w']) && !empty($_POST['w']) && empty($_POST['comment'])) {

$_POST['comment']= $_POST['w'];unset($_POST['w']);

} else {$_POST['spam_confirmed'] = 1;}

}

//處理

function sink($comment) {

if (isset($_POST['spam_confirmed']) && !empty($_POST['spam_confirmed'])) {

//方法一:直接擋掉, 將 die(); 前面兩斜線刪除即可.

//die();

//方法二:標記為spam, 留在資料庫檢查是否誤判.

add_filter('pre_comment_approved',create_function('','return "spam";'));

$is_ping = in_array($comment['comment_type'], array('pingback', 'trackback'));

if ($is_ping) {

$comment['comment_content'] = "◎ 這是 Pingback/Trackback, 小牆懷疑這可能是 Spam!n" .$comment['comment_content'];

} else {

$comment['comment_content'] = "[ 小牆判斷這是Spam! ]n" .$comment['comment_content'];

}}

return $comment;

}}

$anti_spam = new anti_spam();

// -- END ----------------------------------------

?>

简单不?真想大声喊:我爱代码!

十、给主题增加公告栏

这个方法来自主题大师 mg12 的这篇文章《主题技巧:
为主题添加管理选项
》,mg12的例子就是一个公告栏的例子,所以直接用他的例题代码就ok了,不过公告栏的 css 样式就要自己根据自己主题风格写了,id是notice,class是content。

1. 把下面的代码加入所用主题的 Function.php,mg12 的代码里有大量注释,所以不用我多说,如果你只要一个公告功能直接使用就可以了

<?php /** * 选项组类型 */ class ClassicOptions { /* -- 获取选项组 -- */ function getOptions() { // 在数据库中获取选项组 $options = get_option('classic_options'); // 如果数据库中不存在该选项组, 设定这些选项的默认值, 并将它们插入数据库 if (!is_array($options)) { $options['notice'] = false; $options['notice_content'] = ''; // TODO: 在这里追加其他选项 update_option('classic_options', $options); } // 返回选项组 return $options; } /* -- 初始化 -- */ function init() { // 如果是 POST 提交数据, 对数据进行限制, 并更新到数据库 if(isset($_POST['classic_save'])) { // 获取选项组, 因为有可能只修改部分选项, 所以先整个拿下来再进行更改 $options = ClassicOptions::getOptions(); // 数据限制 if ($_POST['notice']) { $options['notice'] = (bool)true; } else { $options['notice'] = (bool)false; } $options['notice_content'] = stripslashes($_POST['notice_content']); // TODO: 在这追加其他选项的限制处理 // 更新数据 update_option('classic_options', $options); // 否则, 重新获取选项组, 也就是对数据进行初始化 } else { ClassicOptions::getOptions(); } // 在后台 Design 页面追加一个标签页, 叫 Current Theme Options add_theme_page("Current Theme Options", "Current Theme Options", 'edit_themes', basename(__FILE__), array('ClassicOptions', 'display')); } /* -- 标签页 -- */ function display() { $options = ClassicOptions::getOptions(); ?> <form action="#" method="post" enctype="multipart/form-data" name="classic_form" id="classic_form"> <div class="wrap"> <h2><?php _e('Current Theme Options', 'classic'); ?></h2> <!-- 公告栏 --> <table class="form-table"> <tbody> <tr valign="top"> <th scope="row"> <?php _e('Notice', 'classic'); ?> <br/> <small style="font-weight:normal;"><?php _e('HTML enabled', 'classic') ?></small> </th> <td> <!-- 是否显示公告栏 --> <label> <input name="notice" type="checkbox" value="checkbox" <?php if($options['notice']) echo "checked='checked'"; ?> /> <?php _e('Show notice.', 'classic'); ?> </label> <br/> <!-- 公告栏内容 --> <label> <textarea name="notice_content" cols="50" rows="10" id="notice_content" style="width:98%;font-size:12px;" class="code"><?php echo($options['notice_content']); ?></textarea> </label> </td> </tr> </tbody> </table> <!-- TODO: 在这里追加其他选项内容 --> <!-- 提交按钮 --> <p class="submit"> <input type="submit" name="classic_save" value="<?php _e('Update Options &raquo;', 'classic'); ?>" /> </p> </div> </form> <?php } } /** * 登记初始化方法 */ add_action('admin_menu', array('ClassicOptions', 'init')); ?>

2. 调用方法直接引用 mg12 原话:要公告栏在首页上显示, 需要修改一下 index.php, 这个比较简单, 只是通过一些判断语句决定东西要不要显示出来而已. 当然, 你可以进行其他操作, 关键是获取到选项的值, 并对它们进行处理。

其实可以分为两步:1. 获取选项 (对每个 PHP 文件, 获取一次就行了, 可以在文件顶部执行) ;2. 对选项进行处理 (这里判断成立的话就将公告内容显示出来)

<!-- 获取选项 --> <?php $options = get_option('classic_options'); ?> <!-- 如果用户选择显示公告栏, 并且公告栏有内容, 则显示出来 --> <?php if($options['notice'] && $options['notice_content']) : ?> <div id="notice"> <div class="content"><?php echo($options['notice_content']); ?></div> </div> <?php endif; ?>

这样就有了一个公告功能,在 wp 后台主题栏多了一个选项,在里面可以添加公告栏内容。

十一、WP 表情符号

省插件的东西,原始作者:酷米小客,修改:willin -> 《WP表情符号》,拿来主义者:zwwooooo,嘻嘻

最近超懒,上传也懒,那么把下面的代码存为 smiley.php 文件,然后放到主题目录里

<script type="text/javascript" language="javascript"> /* <![CDATA[ */ function grin(tag) { var myField; tag = ' ' + tag + ' '; if (document.getElementById('comment') && document.getElementById('comment').type == 'textarea') { myField = document.getElementById('comment'); } else { return false; } if (document.selection) { myField.focus(); sel = document.selection.createRange(); sel.text = tag; myField.focus(); } else if (myField.selectionStart || myField.selectionStart == '0') { var startPos = myField.selectionStart; var endPos = myField.selectionEnd; var cursorPos = endPos; myField.value = myField.value.substring(0, startPos) + tag + myField.value.substring(endPos, myField.value.length); cursorPos += tag.length; myField.focus(); myField.selectionStart = cursorPos; myField.selectionEnd = cursorPos; } else { myField.value += tag; myField.focus(); } } /* ]]> */ </script> <a href="javascript:grin(':?:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_question.gif" alt="" /></a> <a href="javascript:grin(':razz:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_razz.gif" alt="" /></a> <a href="javascript:grin(':sad:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_sad.gif" alt="" /></a> <a href="javascript:grin(':evil:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_evil.gif" alt="" /></a> <a href="javascript:grin(':!:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_exclaim.gif" alt="" /></a> <a href="javascript:grin(':smile:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_smile.gif" alt="" /></a> <a href="javascript:grin(':oops:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_redface.gif" alt="" /></a> <a href="javascript:grin(':grin:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_biggrin.gif" alt="" /></a> <a href="javascript:grin(':eek:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_surprised.gif" alt="" /></a> <a href="javascript:grin(':shock:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_eek.gif" alt="" /></a> <a href="javascript:grin(':???:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_confused.gif" alt="" /></a> <a href="javascript:grin(':cool:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_cool.gif" alt="" /></a> <a href="javascript:grin(':lol:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_lol.gif" alt="" /></a> <a href="javascript:grin(':mad:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_mad.gif" alt="" /></a> <a href="javascript:grin(':twisted:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_twisted.gif" alt="" /></a> <a href="javascript:grin(':roll:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_rolleyes.gif" alt="" /></a> <a href="javascript:grin(':wink:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_wink.gif" alt="" /></a> <a href="javascript:grin(':idea:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_idea.gif" alt="" /></a> <a href="javascript:grin(':arrow:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_arrow.gif" alt="" /></a> <a href="javascript:grin(':neutral:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_neutral.gif" alt="" /></a> <a href="javascript:grin(':cry:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_cry.gif" alt="" /></a> <a href="javascript:grin(':mrgreen:')"><img src="<?php bloginfo('wpurl'); ?>/wp-includes/images/smilies/icon_mrgreen.gif" alt="" /></a> <br />

那么接着打开主题文件 comments.php,在 textarea 之前的适当位置加上:

<?php include(TEMPLATEPATH . '/smiley.php'); ?>

还是那么简单……

几段代码真长,那么 part 2 就到此吧。

**************************************************

用代码武装你的wordpress [part 1]

用代码武装你的wordpress [part 2] <--当前

用代码武装你的wordpress [part 3]

zww
or
oooo

“用代码武装你的wordpress [part 2]”有64条评论

  1. Jalena says:

    表情错误哟,一排红叉叉..

    1. zwwooooo says:

      刚才修改代码,现在ok啦

  2. Jalena says:

    看来你又在折腾...沙发占了,板凳也给你擦擦..

    1. zwwooooo says:

      只是在整理自己目前使用的所有代码并且总结一下

  3. 支持一个 :oops:

    1. zwwooooo says:

      Thanks

  4. A.shun says:

    表情插件可以省掉了 :shock:

    1. zwwooooo says:

      就是这个目的

  5. kangzj says:

    Ctrl + Enter 提交评论 用上了

  6. Louis Han says:

    Ctrl + Enter 原来是这么实现的 不错

    1. zwwooooo says:

      这个是最简单的方法

  7. 老饕 says:

    呵呵,等你折腾完了,我一个一个拿来用~~

    1. zwwooooo says:

      欢迎拿去用,因为我也是拿来的,哈哈

  8. 阿修 says:

    做地板 等Part3~~ :roll:

    1. zwwooooo says:

      part3 也就是评论方面的东东了

  9. waitwho says:

    先学习一下!

  10. Derek says:

    省的到处跑了 :oops:

    1. zwwooooo says:

      对于我来说也是,哈

  11. 小T says:

    最近很忙,折腾不起,一看都是实用的东西,让博客变得更强大

    1. zwwooooo says:

      闲时再折腾吧,都是些我正在用的东东

  12. zrqx008 says:

    Ctrl + Enter这个功能比较实用

    1. zwwooooo says:

      嗯,特别是大家基本都用QQ的情况下

  13. 奋飞 says:

    代码如此之长,
    我真的不看了

  14. 小y says:

    表情代码原来用过,不过好像还要自己改下评论框的ID。。 :?:

    1. zwwooooo says:

      主题的comments正规就不用,表情代码主要考虑后面一大窜表情的地址就ok了,像我的就要加上 wordpress

  15. 林木木 says:

    2篇文章都写得很清晰!支持了

    1. zwwooooo says:

      Copy大法发挥的淋漓尽致…… :grin:

  16. 小毅 says:

    其实,是to be continued :grin:

    1. zwwooooo says:

      说明你看懂了 :mrgreen:

      1. 小毅 says:

        再来坐坐。 :smile:

        1. zwwooooo says:

          part 3 一直起不了头,哈,又懒下了

  17. aunsen says:

    都是好东西!

    1. zwwooooo says:

      请随便CP

  18. mytion says:

    也想整个公告栏,先来做笔记。Z大我来挖宝啦。 :lol:

    1. zwwooooo says:

      慢慢挖慢慢折腾吧 :mrgreen:

  19. 实用,收藏之

  20. 漠天 says:

    太有用了,学习啦

    1. zwwooooo says:

      @漠天 有用请随意CP :mrgreen:

回复给 漠天 ¬
取消回复

昵称 *

网址

B em del U Link Code Quote