Posts tagged ‘Wordpres’

WordPressプラグインの作成

wpでスタイルシートを使用するためのplugin
で、一時的ではない自分のスタイルシートを使いたいという要望があったのですが、
それをかなえるプラグインがなさそうでしたので、作成してみることにしました。

WordPressのプラグイン作成は初めてなので、まず理解しておくことがたくさんあります。

WordPress プラグイン作成時のノウハウ – 前編にわかりやすいように記事がありましたので、それにならって作成したいと思います。

とりあえず作ればよいファイルは、wp-content/pluginsに作るディレクトリ、メイン処理のphpファイルを作ります。
今回は、自分のスタイルシートを追加したいということにして、add-my-cssという名前にしました。add-my-cssディレクトリの中にadd-my-css.phpファイルを作成します。

作成したら中身を考えますが、何も考えずに入れれるのはWordpress標準ヘッダとライセンス事項です。(ここ参照)

こんな感じで

<?php
 
/*
Plugin Name: Add My CSS
Plugin URI: http://blog.4sure.jp/yokoshima/
Description: My Style Sheet is inserted in the header.
Version: 0.1
Author: yokoshima
Author URI:  http://blog.4sure.jp/yokoshima/
*/
/*  Copyright 2009 yokoshima (email : k.yokoshima@4sure.co.jp:)
 
    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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

次に、ちょっと謎の言葉である「フック」ですが、これはWordPressがある処理を行うところを定義しておき、そのタイミングで実行させることを定義しておくものです。
今回の場合、Wordpressがheaderを出力するタイミングでスタイルシート定義を出力したいので、アクションフックの「wp_head」がいいタイミングです。
とりあえずは見よう見まねで、クラスの作成と、アクションフックの登録だけ記載しました。
いかが現時点のソースです。
ヘッダ内に<!– add_my_css 0.1 –>と表示されれば今回はOKです。

<?php
 
/*
Plugin Name: Add My CSS
Plugin URI: http://blog.4sure.jp/yokoshima/
Description: My Style Sheet is inserted in the header.
Version: 0.1
Author: yokoshima
Author URI:  http://blog.4sure.jp/yokoshima/
*/
/*  Copyright 2009 yokoshima (email : k.yokoshima@4sure.co.jp:)
 
    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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
 class Add_My_CSS {
 
        var $version = "0.1";
 
        function Add_My_CSS(){}
 
        function wp_head() {
                echo "\n<!-- add_my_css $this->version -->\n";
        }
}
 
$_add_my_css = new Add_My_CSS();
add_action('wp_head', array($_add_my_css, 'wp_head'));
 
?>

これで、プラグインを有効にすると、<!– add_my_css 0.1 –>と表示されました。
結構簡単にできちゃうんじゃないかな?