Standing on the Shoulder of Linus

Home / 2011 / 2月 / 12 / PHP Snippets for Theme Designer

PHP Snippets for Theme Designer

This plugin displays a php snippet in page list, category list and tag list. You can use conditional tags (is_page, is_category and is_tag) by just copying a snippet.

The page/category/tag id is already placed, so you don’t have to bother.

function readonly_textarea( $data, $rows=3, $cols=30 ) {
    $data = esc_html( $data );
    $rows = intval( $rows );
    $cols = intval( $cols );
    return '<textarea readonly rows="' . $rows . '" cols="' . $cols . '" onclick="this.focus();this.select()">' . $data . '</textarea>';
}

function create_snippet_if( $condition, $id, $slug ) {
    $condition = esc_html( $condition );
    $id = esc_html( $id );
    $slug = esc_html( urldecode( $slug ) );
    $snippetformat = '&lt;?php if ( %1$s ( '%2$s' ) ):/* %3$s */ ?&gt; ' . "nn" . '&lt;?php endif; /* %1$s( '%2$s' ) %3$s */ ?&gt;';
    return sprintf($snippetformat, $condition, $id, $slug);
}

function add_snippet_column( $defaults ) {
    $defaults['phpsnippet'] = __('PHP snippet') ;
    return $defaults;
}
add_filter('manage_pages_columns', 'add_snippet_column');
function add_categories_snippet_column( $defaults ) {
    $defaults = array_merge($defaults,array('phpsnippet' =>'phpsnippet')) ;
    return $defaults;
}
add_filter('manage_edit-category_columns', 'add_categories_snippet_column');
add_filter('manage_edit-post_tag_columns', 'add_categories_snippet_column');

function add_snippet_text($column_name, $id) {
    if( $column_name == 'phpsnippet' ) {
        $pageinfo = get_page( $id );
        $output = create_snippet_if( 'is_page', $pageinfo->ID, $pageinfo->post_name );
        print readonly_textarea( $output );
    }

}
add_action('manage_pages_custom_column', 'add_snippet_text', 10, 2);
function add_categories_snippet_text($null,$column_name, $id) {
    $cat_name = get_cat_name($id);
    $output = create_snippet_if( 'is_category', $id, $cat_name );
    return readonly_textarea($output);
}
add_filter('manage_category_custom_column', 'add_categories_snippet_text', 10, 3);
function add_post_tag_snippet_text($null,$column_name, $id) {
    $tag_array = get_term_by('id',$id,'post_tag');
    $output = create_snippet_if( 'is_tag', $id, $tag_array->name );
    return readonly_textarea($output);
}
add_filter('manage_post_tag_custom_column', 'add_post_tag_snippet_text', 10, 3);

This plugin is based on WordPress 3.1. No guarantee for WordPress 3.0 or older.

Japanese introduction is available.

関連

Posted in myplugin, php | Tagged PHP, スニペット, デザイナー
← OSC香川 テックカフェSAHANA →

アーカイブ

人気の投稿とページ

  • キンドル本を印刷する(PDFに変換する)方法
  • 名古屋駅から国際センターまでの道のり(徒歩)
  • AGPL ライセンス(GPLとは似ているが違いもある)
  • 問い合わせフォーム改善: 選択肢により条件分岐し、項目の表示非表示を変更する
  • JP-Secure SiteGuard WP Pluginは不正ログイン防止に役立つか

プロフィール

水野史土:月70万PVホームページ制作会社のレスキューワーク株式会社で、PHPソフトウェアのサポートを行っている。concrete5コミュニティリーダー、Novius OSコアコード貢献者でもある。 詳しくは管理者詳細参照。
大好評WordPress書籍「WordPressユーザーのためのPHP入門 はじめから、ていねいに。」サポートページ

Copyright © 2015 Standing on the Shoulder of Linus.