自己动手写 WordPress 插件:开始

WordPress 之所以被广泛应用,一个很重要的原因,他很有多插件,多到什么程度呢,就像天上的星星一样多,哈哈,形容得真恶心,自己寒一个。今天开始给大家写一些原创性的文章:自己动手写插件,今天是第一篇,算是一个开始,从一个最简单插件开始,刚刚写好的,厉害吧!也是对阿里妈妈插件大赛的一个支持吧!

一个最简单的插件包括以下两部分:插件头信息和一个可在模板中使用的函数。那我们从第一步头信息开始:

自己动手写插件的第一步就是定义插件头信息:

/* 

Plugin Name: WP 的沙发
Plugin URI: http://blog.wpjam.com/
Description: 本人代表广大 blogger 仅此插件献给沙发党以表彰他们对博客做出的杰出奉献
Version: 沙发一代
Author: 某低调沙发党
Author URI: http://blog.wpjam.com/
*/

这里我们定义了插件名称,版本,作者,下载地址,和简单的介绍。

插件的第二步是定义一个可在模板中使用的模板函数:

我开始的这个沙发插件非常简单,不会含有任何的任何的 filter 和 hook。所以我们这个插件实际上只需要对 WordPress 的 Comment 表进行查询,获取抢得沙发最多的三个留言者即可。

原本这个插件只需一条还算复杂的 SQL 语句即可搞定,但是著名的空间上盘古的 MySQL 的版本还是 4.0,泪奔,不支持 SQL 嵌套。算了还是贴出我写得很菜的 SQL 语句:

SELECT * , count( first_comments.comment_author ) AS comment_comments FROM ( SELECT * FROM ( SELECT comment_author, comment_post_id, comment_author_url FROM wp_comments WHERE comment_type = '' AND user_id =0 AND comment_approved = '1' ORDER BY comment_date )ordered_comments GROUP BY ordered_comments.comment_post_id )first_comments GROUP BY first_comments.comment_author ORDER BY comment_comments DESC LIMIT 3

既然不支持,按只有自己写了代码了,创建一个叫做 sofa 的函数。程序的代码非常简单,基本的逻辑如下:

    查出含有留言的 Post 的 ID。
    然后找出它们的第一条留言的留言者和其博客,帮把这些信息写到一个数组中。
    对数组进行处理找出抢到沙发的三个留言者。
    输出他们。

具体代码:

function sofa(){
	global $wpdb;

	$first_commentors = array(); //初始化沙发党数组

	$q = "SELECT DISTINCT comment_post_id FROM $wpdb->comments WHERE comment_type ='' AND user_id = 0 AND comment_approved = '1'"; 
	$have_comment_post_ids = $wpdb->get_results($q); //获取有留言的日志ID
	foreach ($have_comment_post_ids as $have_comment_post_id){
		$q = "SELECT comment_author FROM $wpdb->comments WHERE comment_type ='' AND user_id = 0 AND comment_approved = '1' AND comment_post_id = $have_comment_post_id->comment_post_id order by comment_date limit 1";
		$first_comment = $wpdb->get_results($q);  //获取沙发党
		array_push($first_commentors,$first_comment[0] -> comment_author); //添加进沙发党数组
	}
	
	$first_commentors = (array_count_values ($first_commentors)); //统计
	arsort($first_commentors); //排序
	
	$first_commentors_author = array_keys($first_commentors);//获取沙发党名字
	
	$output = ""; //初始化输出字符串
	$output .= '<ul class="wp_sofa">';
	for($i=0; $i<3; $i++){
		$title="";
		if($i==0)$title = "状元:";
		if($i==1)$title = "榜眼:";
		if($i==2)$title = "探花:";
		$q = "SELECT comment_author_url FROM $wpdb->comments WHERE comment_type ='' AND user_id = 0 AND comment_approved = '1' AND comment_author_url !='' AND comment_author = '$first_commentors_author[$i]' limit 1";
		$first_comment_url = $wpdb->get_results($q); //获取沙发党的博客
		if($first_comment_url){
			$output .= '<li><a href="' . $first_comment_url[0] -> comment_author_url . '" title="' . $first_commentors_author[$i].'">' . $title.$first_commentors_author[$i] . '</a>(' . $first_commentors["$first_commentors_author[$i]"].')</li>';
		} else {
			$output .= '<li>' . $title.$first_commentors_author[$i] . '('.$first_commentors["$first_commentors_author[$i]"] . ')</li>';
		}
	}
	$output .= '</ul>';
	echo $output;//输出沙发党前三甲
}
?>

然后在模板上(一般是在侧边拦侧)调用这个 sofa 函数即可。调用的方法为:

<?php if(function_exists('sofa')) { sofa(); } ?>

我们自己动手写插件的第一篇就到此为止,有任何问题请留言,但是抢沙发没有奖。
下一节我们讲讲如何窗体化(Widgetize)该插件,请大家保持关注,本人首次尝试写教程,如有不好的地方,欢迎大家提出批评和意见。

该插件演示请看本博客侧边栏订阅的下面!

这篇教程所实现的插件下载地址:WP-Sofa。并以此插件献给某著名沙发党大猫和胡戈戈同学。

标签:WordPress 插件 WordPress 教程 沙发

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号