用代码武装你的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. 羽中 says:

    不错不错,我选好的一个个搬到typecho;)

    1. zwwooooo says:

      @羽中 荒淫搬,听说typecho挺不错,中国的wp

  2. 万能的博主啊,“Ctrl + Enter 提交评论”只见滚动条,不见代码啊!

    PS:万戈推荐,特来围观!

    1. zwwooooo says:

      @快乐岛博客 1. 我不是万能的博主

      2. 你用其他浏览器(如ff、chrome、opera),别用ie(包括ie内核)的浏览器

      3. 我稍后试着改一下css,看能否兼容那该死的ie

  3. 博主,你评价里的楼层是怎么弄出来的?太好看了

    1. zwwooooo says:

      @快乐岛博客 我用的是 jQuery,把所有前提堆积到第二层,不错吧,Jinwen的想法,我写过文章但没写方法,比较麻烦 http://zww.me/archives/24737 ,Jinwen那有基本思路和方法

  4. 罗子夏 says:

    站长你好,我的网站也是WordPress的,用的主题不支持侧边栏小工具,我想要一个标签云的代码,站长这个可以实现吗?

    谢谢站长解答 :smile:

    1. zwwooooo says:

      @罗子夏

      <?php wp_tag_cloud('smallest=9&largest=18'); ?>

  5. BoKeam says:

    折腾啊折腾

    1. zwwooooo says:

      @BoKeam 因为喜欢

  6. 最近折腾wordpress的一些小结 – Perfume's space says:

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

  7. wei says:

    还有个问题想请教一下:“七、Ctrl + Enter 提交评论”这个方法我试了之后,发现在firefox下能用,用chrome之后,Ctrl+Enter没反应,不知道是什么原因?另外,我用了willin大师的ajax评论的那个,不知道有没有冲突?

    1. zwwooooo says:

      @wei 你试试我的博客就知道了。(willin的ajax评论+ctrl enter提交)
      所以代码是没问题的。

    2. wei says:

      @zwwooooo 发现自己弄错了,在VBox里右Ctrl键是无效的 :!: :!: :!:

  8. 测试一下ctrl+enter

    1. zwwooooo says:

      @博客·琼斯 这功能已经“泛滥”了 :mrgreen:

    2. 爱新奇 says:

      @zwwooooo
      哈 如果是人性化的功能 泛滥也无妨

    3. zwwooooo says:

      @爱新奇
      所以加了双引号

  9. [...] 改方法来自zwwooooo的用代码武装你的wordpress [part 2]一文 [...]

  10. [...] 7.添加功能如Ctrl + Enter 提交评论、防 Spam 代码:WP Anti Spam 小牆 1.7(如果不管用将启用Akismet)、无需插件添加WP 表情符号(注意表情文件路径问题) 参考文章:用代码武装你的wordpress [part 2] [...]

  11. [...] 先看看原来的 Ctrl + Enter 提交评论代码(注意红色部分): 1 <textarea name="comment" id="comment" cols="100%" rows="6" tabindex="4" onkeydown="if(event.ctrlKey&&event.keyCode==13){document.getElementById('submit').click();return false};"></textarea> [...]

  12. 代码都坏了?看不到啊。。。怎么回事,刷很多次了。

    1. zwwooooo says:

      @yesureadmin
      啥?木有问题啊

  13. [...] 以上内容全部来自Zww.me [...]

  14. [...] 代码效果:本页下方 代码来自:ZWW,本主题集成,则无须添加smiley.php [...]

回复给 博客·琼斯 ¬
取消回复

昵称 *

网址

B em del U Link Code Quote