小さなコード集

【最終更新日:2011年12月23日】

公開された「dogmap.jp::WordPress でスニペットを簡単に管理する方法」を適用していて、WordPress の plugins ディレクトリ下の my-hacks 内にアップロードしています(詳細は、「Portfolio」の小さなコードの管理方法を参照)。

以下、2011年12月23日現在利用している Hacks をリストします。

  1. コンテンツの一部のみを非公開とするプラグイン
  2. 画像を挿入するときに相対リンクにしてくれるプラグイン
  3. URL を記述しただけでウェブのスクリーンショットを撮るプラグイン
  4. 投稿記事のエディター用のフォントを変更するプラグイン
  5. Feedback CHampuru のセルフ Tweet を避けるためのプラグイン

コンテンツの一部のみを非公開とするプラグイン

本サイト利用する稼働は定かではないのですが・・・それでも非常に利用価値がありそうですのでインストールしちゃいました!「firegoby::WordPressでコンテンツの一部だけに認証をかける」が参考サイトです。利用するには、ショートコードが必要です。

<?php
// コンテンツの一部のみを非公開とするプラグイン
// 参考サイト: http://firegoby.theta.ne.jp/archives/2394

function login_read_more($atts, $content = null) {
    if (is_user_logged_in() && !is_null( $content ) && !is_feed()) {
        return $content;
    } else {
        return '続きを読むにはログインが必要です・・・';
    }
}
add_shortcode('auth', 'login_read_more');
?>

画像を挿入するときに相対リンクにしてくれるプラグイン

WebMatrix 上の WordPress で投稿記事を作成して公開する場合、ライブラリーのリンクがドメインを引きずって修正するのがめんどくさい!なんて考えていたらしっかり修正してくれるコードが「Simple Colors::WordPressで挿入する画像のsrcを相対リンクにする」で公開されていました!素晴らしい!!

<?php
// 画像を挿入するときに相対リンクにしてくれるプラグイン
// 参考サイト: http://www.warna.info/archives/20/

function delete_host_from_attachment_url( $url ) {
    $regex = '/^http(s)?://[^/s]+(.*)$/';
    if ( preg_match( $regex, $url, $m ) ) {
        $url = $m[2];
    }
    return $url;
}
add_filter( 'wp_get_attachment_url', 'delete_host_from_attachment_url' );
add_filter( 'attachment_link', 'delete_host_from_attachment_url' );
?>

URL を記述しただけでウェブのスクリーンショットを撮るプラグイン

「 デザインどや!?::どや!?いつか使うかも知れない簡単に出来るWordPressカスタマイズまとめ」を参考にしているプラグインです。投稿記事や固定ページ内でショートコード を用います。

<?php
// URL を記述しただけでウェブのスクリーンショットを撮るプラグイン
// 参考サイト:http://www.doya-doya.com/word-press/2011/08/19/7785
function wpr_snap($atts, $content = null) {
        extract(shortcode_atts(array(
            "snap" => 'http://s.wordpress.com/mshots/v1/',
            "url" => 'http://www.catswhocode.com',
            "alt" => 'My image',
            "w" => '400', // width
            "h" => '300' // height
        ), $atts));
    $img = '<img src="' . $snap . '' . urlencode($url) . '?w=' . $w . '&h=' . $h . '" alt="' . $alt . '"/>';
        return '<a href="' . $url . '">' . $img . '</a>';
}
add_shortcode("snap", "wpr_snap");
?>

とにかく頻繁に利用している Hack の内の一つです。

投稿記事のエディター用のフォントを変更するプラグイン

WordPress 3.3 にアップグレードすると以前の変更が無効になってしまったので、投稿記事「[M] mbdb::WordPress 3.3の投稿画面のフォントを変更する10行のコード」を参考に以下のコードに修正しました。

<?php
// 投稿記事のエディター用のフォントを変更するプラグイン
// 参考サイト:http://www.catswhocode.com/blog/8-new-and-amazing-wordpress-hacks

add_action( 'admin_head-post.php', 'cwc_fix_html_editor_font' );
add_action( 'admin_head-post-new.php', 'cwc_fix_html_editor_font' );

function cwc_fix_html_editor_font() { ?>

<style type="text/css">#wp-content-editor-container textarea { font-family:メイリオ,Meiryo,"ヒラギノ角ゴ Pro W3","Hiragino Kaku Gothic Pro","MSPゴシック","MS P Gothic",Osaka,Verdana,Arial,Helvetica,sans-serif; }</style>

<?php }
?>

「CatsWhoCode.com::8 New and amazing WordPress hacks」を参考に以下のコードで実現しています。フォントは個人の好みで変更することができます。

<?php
// 投稿記事のエディター用のフォントを変更するプラグイン
// 参考サイト:http://www.catswhocode.com/blog/8-new-and-amazing-wordpress-hacks

add_action( 'admin_head-post.php', 'cwc_fix_html_editor_font' );
add_action( 'admin_head-post-new.php', 'cwc_fix_html_editor_font' );

function cwc_fix_html_editor_font() { ?>

<style type="text/css">#editorcontainer #content, #wp_mce_fullscreen { font-family:メイリオ,Meiryo,"ヒラギノ角ゴ Pro W3","Hiragino Kaku Gothic Pro","MSPゴシック","MS P Gothic",Osaka,Verdana,Arial,Helvetica,sans-serif; }</style>

<?php }
?>

目立ちませんが、好きなフォントを利用することでどんどん投稿記事を書きたくなります!?

Feedback Champuru のセルフ Tweet を避けるためのプラグイン

公開されている「Feedback Champuru」用の補助機能です。「dogmap.jp::Feedback Champru」を参考にしていますが、「Feedback Champuru」を有効化していなければ機能しません。現在は無効化しています。

<?php
// Feedback Champuru のセルフ Tweet を避けるためのプラグイン
// 参考サイト:http://dogmap.jp/2010/07/04/feedback-champru/

function champru_comments($comments_array, $type){
        $comments = array();
        foreach ($comments_array as $comment) {
            switch ($type){
            case 'tweet':
            case 'hatena':
            case 'delicious':
                if ($comment->comment_author != 'zerochacool')
                    $comments[] = $comment;
                break;
            default:
                $comments[] = $comment;
                break;
            }
        }
        return $comments;
}
add_filter('feedback-champuru/comments_array', 'champru_comments', 10, 2);
?>

【公開日:2011年09月18日】