WordPress: rel=”canonical” を追加する「wp-canonical mod by hide10」プラグイン
複数の類似した内容のページの中で、優先されるページを示すメタタグ「rel="canonical"」を追加するプラグインを公開します。
ダウンロード
このプラグインは、「wp-canonical」として公開されていたプラグインを、当ブログのURL形式にあうよう修正を加えたものです。
他のURL形式を持つブログでは、生成されるcanonical URLが適切ではない場合があるので注意してください。
ダウンロード: wp-canonical mod by hide10
元ファイル
修正元となった wp-canonical も置いときます。
元となったプラグイン「wp-canonical」は、「All in One SEO Pack」にソースマージされています。
もしあなたがAll in One SEO Packを使っているなら、このプラグインを使う必要はありません。
ダウンロード: wp-canonical.zip
ソース
以下、wp-canonical mod by hide10のソースです。
<?php
/*
Plugin Name: Canonical URL's mod by hide10
Version: 0.2
Plugin URI: https://www.hide10.com/archives/17148
Description: Adds rel="canonical" links to the head of your site.
Author: KOBAYASHI Hideto
Author URI: https://www.hide10.com/
Copyright 2012 KOBAYASHI Hideto (email: hide10@hide10.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
function yoast_get_paged($link) {
$page = get_query_var('paged');
if ($page && $page > 1) {
$link = trailingslashit($link) ."page/". "$page";
}
return $link;
}
function yoast_guess_url($query) {
if ($query->is_404 || $query->is_search) {
return false;
}
$haspost = count($query->posts) > 0;
$has_ut = function_exists('user_trailingslashit');
// Copied entirely and slightly modified from Scott Yang's Permalink Redirect, http://svn.fucoder.com/fucoder/permalink-redirect/
if (get_query_var('m')) {
$m = preg_replace('/[^0-9]/', '', get_query_var('m'));
switch (strlen($m)) {
case 4: // Yearly
$link = get_year_link($m);
break;
case 6: // Monthly
$link = get_month_link(substr($m, 0, 4), substr($m, 4, 2));
break;
case 8: // Daily
$link = get_day_link(substr($m, 0, 4), substr($m, 4, 2),
substr($m, 6, 2));
break;
default:
return false;
}
} elseif ($query->is_author && $haspost) {
global $wp_version;
if ($wp_version >= '2') {
$author = get_userdata(get_query_var('author'));
if ($author === false)
return false;
$link = get_author_link(false, $author->ID,
$author->user_nicename);
} else {
// XXX: get_author_link() bug in WP 1.5.1.2
// s/author_nicename/user_nicename/
global $cache_userdata;
$userid = get_query_var('author');
$link = get_author_link(false, $userid,
$cache_userdata[$userid]->user_nicename);
}
} elseif ($query->is_category && $haspost) {
$link = get_category_link(get_query_var('cat'));
$link = yoast_get_paged($link);
} else if ($query->is_tag && $haspost) {
$tag = get_term_by('slug',get_query_var('tag'),'post_tag');
if (!empty($tag->term_id)) {
$link = get_tag_link($tag->term_id);
}
$link = yoast_get_paged($link);
} elseif ($query->is_day && $haspost) {
$link = get_day_link(get_query_var('year'),
get_query_var('monthnum'),
get_query_var('day'));
} elseif ($query->is_month && $haspost) {
$link = get_month_link(get_query_var('year'),
get_query_var('monthnum'));
} elseif ($query->is_year && $haspost) {
$link = get_year_link(get_query_var('year'));
} elseif ($query->is_home) {
if ((get_option('show_on_front') == 'page') &&
($pageid = get_option('page_for_posts'))) {
$link = get_permalink($pageid);
$link = yoast_get_paged($link);
$link = trailingslashit($link);
} else {
if ( function_exists( 'icl_get_home_url' ) ) {
$link = icl_get_home_url();
} else {
$link = get_option( 'home' );
}
$link = yoast_get_paged($link);
}
} elseif ($query->is_tax && $haspost ) {
$taxonomy = get_query_var( 'taxonomy' );
$term = get_query_var( 'term' );
$link = get_term_link( $term, $taxonomy );
$link = yoast_get_paged( $link );
} elseif ( $query->is_archive && function_exists( 'get_post_type_archive_link' ) && ( $post_type = get_query_var( 'post_type' ) ) ) {
$link = get_post_type_archive_link( $post_type );
$link = yoast_get_paged( $link );
} else {
return false;
}
return $link;
}
function yoast_canonical_link() {
global $wp_query;
$url = yoast_guess_url($wp_query);
if ($url) {
echo "\n\t".'<link rel="canonical" href="'.$url.'"/>'."\n";
}
}
add_action('wp_head','yoast_canonical_link',10,1);
?>
そんな感じで!





ディスカッション
コメント一覧
まだ、コメントがありません