wordpressのfunctions.phpのいろいろ


wordpressのfuntionsには決まって実装するものがいくつかあります。とりあえずまとめておきました。

  • カスタムフィールドは custom_field(‘フィールド名’) または custom_field_with_id($post->ID, ‘フィールド名’) という風に取得ができます。
  • 一般的に生成されるギャラリーのCSSを無効にしています。
  • 投稿に画像を追加する際にwidth/heightを取り除いています。
  • wp_headの不要なタグを削除しています。
  • Contact form 7では_confirmをつけることでメールの確認などの確認項目が簡単に追加ができます。
  • get_gravatarではauthor.phpなどで投稿者のアイコンに利用できます。

/* アイキャッチサイズ */
set_post_thumbnail_size(900, 9999, true);

/* アップロード画像のサイズ */
add_image_size('size-l', 1024, 999);
add_image_size('square', 620, 620, true);        

/* アイキャッチ有効 */
if ( function_exists('add_theme_support') ) { 
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 340, 999 );
}

/* experptの続きの文字 */
function new_excerpt_more($more) {
      return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');

/* 画像を投稿する際に自動投稿されるwidthとheightを削除し特定のclass名を追加する */
add_filter( 'post_thumbnail_html', 'edit_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'edit_width_attribute', 10 );
function edit_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   $html = preg_replace( '/<a href="(.*)">/', '<a href="$1" class="prettybox">', $html );
   return $html;
}

/* カスタムフィールド関係 */
function get_custom_field_with_id($id, $keyword, $single=true)
{
    return get_post_meta($id, $keyword, $single);
}
function get_custom_field($keyword, $single=true)
{
    return get_post_meta(get_the_ID(), $keyword, $single);
}

function get_thumbnail_tag($post, $size, $empty_show=true)
{
    $custom_thumbnail = get_post_thumbnail_id($post->ID); 
    $images = wp_get_attachment_image_src($custom_thumbnail, $size, true);
    
    /* catche an empty */
    if($images[1] <= 50){
        if($empty_show) /* please change a thumbnail path */
            return '<img src="'.get_bloginfo('template_url').'/img/thumbnail.png" alt="'.get_the_title($post->ID).'" />';
        else return '';
        
    } else return '<img src="'.$images[0].'" alt="'.get_the_title($post->ID).'" />';
    
}
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
    $url = 'http://www.gravatar.com/avatar/';
    $url .= md5( strtolower( trim( $email ) ) );
    $url .= "?s=$s&d=$d&r=$r";
    if ( $img ) {
        $url = '<img src="' . $url . '"';
        foreach ( $atts as $key => $val )
            $url .= ' ' . $key . '="' . $val . '"';
        $url .= ' />';
    }
    return $url;
}

/* Contact form 7 */
add_filter( 'wpcf7_validate_email', 'wpcf7_text_validation_filter_extend', 11, 2 );
add_filter( 'wpcf7_validate_email*', 'wpcf7_text_validation_filter_extend', 11, 2 );
function wpcf7_text_validation_filter_extend( $result, $tag ) {
    $type = $tag['type'];
    $name = $tag['name'];
    $_POST[$name] = trim( strtr( (string) $_POST[$name], "\n", " " ) );
    if ( 'email' == $type || 'email*' == $type ) {
        if (preg_match('/(.*)_confirm$/', $name, $matches)){
            $target_name = $matches[1];
            if ($_POST[$name] != $_POST[$target_name]) {
                $result['valid'] = false;
                $result['reason'][$name] = '確認用のメールアドレスが一致していません';
            }
        }
    }
    return $result;
}

/* ギャラリー */
add_filter(
    "use_default_gallery_style",
    "disable_default_gallery_style"
); 
function disable_default_gallery_style() {
    return false;
}

/* wp-head */
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
remove_action( 'wp_head', 'wp_print_styles', 8 );
remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'rel_canonical' );
remove_action( 'wp_head', 'wp_shortlink_wp_head');

  • このエントリーをはてなブックマークに追加

コメントをどうぞ

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です