WordPress maaguu (^^; com 北海道札幌でホームページ制作を楽しむ

WordPress, WordPress MU(μ) についての話題、プラグイン作成、カスタマイズ。

カテゴリー「plugins」の記事

ユーザエージェント(User Agent、Webブラウザなど)を判別し、別ページを表示する[Permanent Link]

Internet Explorer 6を判別して別ページを表示するプラグインのサンプル。

IE6ハックとかやりたくないので作ってみました。「IE6には対応してないよ、ごめんなさい」というページを表示するだけ。

<?php
/*
Plugin Name: maaguu_replace_page.php
Plugin URI: http://wp.maaguu.com/2008/12/15/replace-page/
Description:
Version: 1.0
Author: maaguu
Author URI: http://maaguu.com/
*/

/*  Copyright 2008 maaguu (email : info@mx.maaguu.com)

    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 maaguu_replacePage
{

    private $userAgent;
    private $pattern_ie6;

    public function __construct()
    {
        $this->userAgent = $_SERVER['HTTP_USER_AGENT'];
        $this->pattern_ie6 = '/MSIE 6/';
        add_action('template_redirect', array($this, 'replace_page'));
    }

    public function replace_page()
    {
        if (preg_match($this->pattern_ie6, $this->userAgent))
        {
            include(TEMPLATEPATH . '/ie6.php');
            exit;
        }
    }

}

new maaguu_replacePage();

/* End of file maaguu_replace_page.php */

プラグインを有効にして、テンプレートディレクトリにie6.phpを置きます。

ポイントを現金でキャッシュバック

bloginfo(‘admin_email’)のメールアドレスを数値文字参照に変換する[Permanent Link]

WordPressのテンプレートにbloginfo('admin_email')と書くと、メールアドレスがそのまま書き出されます。メールアドレス収集ロボットのこともありまして、それはちょっと困ったということで、これを数値文字参照に変換してしまいます(数値文字参照が有効かどうかはさておき、ほんの少しの精神的安定のため)。

<?php
/*
Plugin Name: maaguu_replace_admin_email.php
Plugin URI: http://wp.maaguu.com/2008/12/12/replace-admin-email/
Description:
Version: 1.0
Author: maaguu
Author URI: http://maaguu.com/
*/

/*  Copyright 2008 maaguu (email : info@mx.maaguu.com)

    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 maaguu_replace_admin_email
{
    private $replace_admin_email;

    public function __construct()
    {
        $this->replace_admin_email;
        add_filter('option_admin_email', array($this, 'replace_admin_email'));
    }

    public function replace_admin_email($email)
    {
        for ($i = 0; $i < strlen($email); $i++)
        {
            $this->replace_admin_email .= '&#' . ord($email[$i]) . ';';
        }
        return $this->replace_admin_email;
    }
}

new maaguu_replace_admin_email();

/* End of file maaguu_admin_email.php */

プラグインディレクトリに置いて有効にするだけで使えます。

しかしここで問題が。

headのメールアドレスは変換されていますが、プラグインで書き出したフッタが変換されません(^^ゞ どうしようかなあ……。

というより、このプラグイン、動作がおかしいですので使わないほうがよさげ(^^ゞ

ポイントを現金でキャッシュバック

「WordPress MUのフッタを共用化したい」プラグインのコードってどうよ?[Permanent Link]

WordPress MUのフッタを共用化したいのコードって、変です。

普通は多分、以下のような感じに、変数や定数を初期化するはずです。あと、グローバル変数が入ってるのはまずい。

class Object
{
    private $var;

    public function __construct($var)
    {
        //初期化
        $this->var = $var;
    }

    public function getVar()
    {
        return $this->var;
    }
}

$object = new Object('hogehoge');
echo $object->getVar();

ということで、修正してみます。

<?php
/*
Plugin Name: maaguu_footer.php
Plugin URI: http://wp.maaguu.com/2008/12/11/shareing-footer-2/
Description:
Version: 1.1
Author: maaguu
Author URI: http://maaguu.com/
*/

/*  Copyright 2008 maaguu (email : info@mx.maaguu.com)

    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
*/

$admin_email = get_bloginfo('admin_email');
$footer_html = "
    <div id=\"ft\" class=\"c\">

        <div id=\"link\">
            <h2>link</h2>
            <ul>
                <li><a href=\"http://d.hatena.ne.jp/maa\">maaguu (^^; memo</a></li>
            </ul>
        </div>
        <!-- #link -->

        <address>
            Email to maaguu:<a href=\"mailto:" . $admin_email . "\">" . $admin_email . "</a>
            Powered by <a href=\"http://mu.wordpress.org/\">WordPress MU</a>
        </address>

    </div>
    <!-- #ft -->

</div>
<!-- #wp -->

<script type=\"text/javascript\">
var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");
document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));
</script>
<script type=\"text/javascript\">
try {
var pageTracker = _gat._getTracker(\"UA-200597-8\");
pageTracker._trackPageview();
} catch(err) {}
</script>
</body>
</html>
";

class maaguu_footer
{
    private $admin_email;
    private $footer_html;

    public function __construct($admin_email, $footer_html)
    {
        $this->admin_email = $admin_email;
        $this->footer_html = $footer_html;
        add_action('get_footer', array($this, 'echo_footer'));
    }

    public function echo_footer()
    {
        echo $this->footer_html;
    }
}

new maaguu_footer($admin_email, $footer_html);

/* End of file maaguu_footer.php */

こんな感じでどうでしょうか。

private $admin_email;$this->admin_email = $admin_email;は不要だ(^^ゞ

ポイントを現金でキャッシュバック
Email to maaguu:info@mx.maaguu.com Powered by WordPress MU Clicky Web Analytics