插件源于需求。——Charles
我有一个私人博客,目前使用的WordPress的development version,该版本可以提供WordPress的一键升级。各位WPer应该是有福了,不过呢,这个一键升级,却给我带来一点小麻烦。
我选用了default主题,而这个主题是WP的一部分,所以,每当一键升级的时候,就会使得我自定义的样式被覆盖>.< ,太糟糕了。两次,粗心大意,我于是乎决定,开发个插件,摆平它!
其实很简单的啦。上次,我们使用了一个filter,还记得不,是the_content。这次,正好,我们就用到了一个action。这个action叫做wp_head,是在模板的header.php加载接近结束时候触发的,具体位置视模板不同而有所不同。但是,有一点是确定的,就是wp_head,会在</head>标签之前被触发。
这个action的作用,就是专门在模板中添加插件需要使用的css或者javascript文件的。正好,我利用这个action,将我自己的自定义css文件给插入到当前使用的模板中。
贴点代码吧
<?php
/*
Plugin Name: Insert Custom CSS
Plugin URI: http://sexywp.com/insert-custom-css.htm
Description: This plugin will insert a custom css file to your current theme.
Version: 8.8.21
Author: Charles Tang
Author URI: http://sexywp.com/
*/
/*
Copyright 2008 Charles Tang (email : )
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
function insert_custom_css(){
$css_url = WP_CONTENT_URL . '/plugins/insert-custom-css/style.css';
echo '<link rel="stylesheet" href="' . $css_url . '" type="text/css" media="screen" />';
}
add_action('wp_head','insert_custom_css');
?>
/*
Plugin Name: Insert Custom CSS
Plugin URI: http://sexywp.com/insert-custom-css.htm
Description: This plugin will insert a custom css file to your current theme.
Version: 8.8.21
Author: Charles Tang
Author URI: http://sexywp.com/
*/
/*
Copyright 2008 Charles Tang (email : )
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
function insert_custom_css(){
$css_url = WP_CONTENT_URL . '/plugins/insert-custom-css/style.css';
echo '<link rel="stylesheet" href="' . $css_url . '" type="text/css" media="screen" />';
}
add_action('wp_head','insert_custom_css');
?>
很简短吧,真正的代码只有四行噢~~
非常好,帮助我修正了自己插件的bug:http://niaolei.org.cn/posts/4012
听起来很棒!我写得文章还是有用的~
错啦,真正的代码只有1行。add_action("wp_head","your function")。嘿嘿。
囧,好吧,只有1行……