Posted on

製作WordPress的第一個Plugins開發

1. 在wp-content\plugins下隨意新增一資料夾,建立一隻php檔案,在這邊我是命名為Claire.php
2. 在Claire.php裡加入下列程式碼

<?php
/*
Plugin Name: Claire's PlugIn
Version: 0.1
Description: 小佳的第一個插件
Author: ClaireChang
Author URI: http://claire-chang.com
Plugin URI: http://claire-chang.com
*/

/* 版本檢查 */
global $wp_version;
$exit_msg='小佳的插件適用於wordpress2.5以上';
if (version_compare($wp_version,"2.5","<")){
	exit($exit_msg);
}

/*產生FB按讚連結*/
function claireLink()
{
	global $post;
	$link=urlencode(get_permalink($post->ID));
	$title=urlencode($post->post_title);
	$text=urlencode(substr(strip_tags($post->post_content),
	0, 350));

	return '<div id="fb-root"></div>
	  <script src="http://connect.facebook.net/zh_TW/all.js#appId=&amp;xfbml=1"></script>
	  <fb:like href="'.$link.'" send="false" width="450" show_faces="true" font=""></fb:like>';
}

/* 將按讚連結加至文章底下 */
function claireFilter($content){
	return $content.claireLink();
}

/* 增加hook */
add_filter('the_content', 'claireFilter');
?>

然後至後台的=>外掛,就可以看到剛剛新增的外掛的資訊了!
2013-03-29_122704
將之啟用,變可以在文章的底下,看到fb的按讚連結了!