diff --git a/class/mtee-canonical-setting.php b/class/mtee-canonical-setting.php new file mode 100644 index 0000000..282e814 --- /dev/null +++ b/class/mtee-canonical-setting.php @@ -0,0 +1,75 @@ +options = $options; + if ($this->options['enabled'] == 1) { + add_action('admin_menu', array($this, 'add_meta_fields')); + add_action('save_post', array($this, 'save_meta_fields')); + } + } + + public function add_meta_fields() { + //add_meta_box(表示される入力ボックスのHTMLのID, ラベル, 表示する内容を作成する関数名, 投稿タイプ, 表示方法) + $default_built_ins = array( + 'posts' => array('page', 'post',), + ); + + foreach ($default_built_ins['posts'] as $built_in) { + add_meta_box( + 'canonical_setting', + 'Canonical設定', + array($this, 'insert_meta_fields'), + $built_in, + 'normal' + ); + } + + if (isset($this->options['custom_posts'])) { + foreach ($this->options['custom_posts'] as $custom_post => $post_chk) { + if ($post_chk == 1) { + add_meta_box( + 'canonical_setting', + 'Canonical設定', + array($this, 'insert_meta_fields'), + $custom_post, 'normal' + ); + } + } + } + } + + // カスタムフィールドの入力エリア + public function insert_meta_fields() { + global $post; + echo '
'; + } + + // カスタムフィールドの値を保存 + public function save_meta_fields($post_id) { + if (!empty($_POST[MTEE_CANONICAL_URL])) { //meta_keywordsが入力されている場合 + update_post_meta($post_id, MTEE_CANONICAL_URL, $_POST[MTEE_CANONICAL_URL]); //値を保存 + } else { //未入力の場合は値を削除 + delete_post_meta($post_id, MTEE_CANONICAL_URL); + } + } + } + +} \ No newline at end of file diff --git a/class/mtee-output.php b/class/mtee-output.php index 418ab97..99dbdb1 100644 --- a/class/mtee-output.php +++ b/class/mtee-output.php @@ -23,6 +23,8 @@ if (!class_exists('mtee_meta_output')) { $this->description = $this->site_name . MTEE_META_DESC_PARTICLE; add_action('wp_head', array($this, 'post_output_add_metas')); add_action('wp_head', array($this, 'set_noindex_nofollow')); + add_action('wp_head', array($this, 'set_canonical_url')); + } //-------------------------------------------------------------------- @@ -102,7 +104,6 @@ if (!class_exists('mtee_meta_output')) { public function create_default_keywords_description(): array { $keywords = $this->create_default_keywords(); $this->create_default_description(); - $meta_data = array( MTEE_NAME_KEYWORDS => implode(',', $keywords), MTEE_NAME_DESCRIPTION => $this->description, @@ -112,6 +113,8 @@ if (!class_exists('mtee_meta_output')) { } private function create_default_keywords() { + $type = ''; + $title = ''; if (is_category()) { $type = 'category'; $title = single_cat_title('', false); @@ -349,5 +352,69 @@ if (!class_exists('mtee_meta_output')) { ); } + //-------------------------------------------------------------------- + // canonical + //-------------------------------------------------------------------- + public function set_canonical_url() { + global $post; + + $type = ''; + $id = null; + if (is_post_type_archive()) { + $type = 'custom_post_archive'; + } elseif (is_category()) { + $id = get_query_var('cat'); + $type = 'tax'; + } elseif (is_tag()) { + $id = get_query_var('tag_id'); + $type = 'tax'; + } elseif (is_tax()) { + $id = get_queried_object()->term_id; + $type = 'tax'; + } elseif (is_home() || is_front_page()) { + $type = 'top_page'; + } elseif (is_page() || is_single()) { + $id = $post->ID; + $type = 'posts'; + } + + echo $this->get_canonical_data($id, $type); + + } + + private function get_canonical_data($id, $type) { + if (empty($type)) { + return; + } + $canonical = ''; + + if ($type == 'top_page') { + $canonical = get_option('_mtee')['top_page']['canonical']; + } elseif ($type == 'custom_post_archive') { + $custom_post_name = get_post_type_object(get_post_type())->name; + $canonical = get_option('_mtee')['custom_post'][$custom_post_name]['canonical']; + } elseif ($type == 'posts') { + $canonical = get_post_meta($id, MTEE_CANONICAL_URL, true); + if (empty($canonical)) { + $canonical = get_permalink(); + global $page, $paged; + if ($paged >= 2 || $page >= 2) { + $canonical .= 'page/' . max($paged, $page) . '/'; + } + } + } + +// $noindex = get_post_meta($id, MTEE_NAME_NOINDEX, true); +// $nofollow = get_post_meta($id, MTEE_NAME_NOFOLLOW, true); +// } elseif ($type == 'tax') { +// $noindex = get_term_meta($id, MTEE_NAME_NOINDEX, true); +// $nofollow = get_term_meta($id, MTEE_NAME_NOFOLLOW, true); +// } + + if ($canonical) { + return '' . PHP_EOL; + } + } + } } \ No newline at end of file diff --git a/class/mtee.php b/class/mtee.php index 023b9f8..c9352be 100644 --- a/class/mtee.php +++ b/class/mtee.php @@ -64,26 +64,17 @@ if (!class_exists('MTEE')) { return get_option('_mtee'); } - public function is_enable(): bool { - if (isset($this->get_options()['enabled']) - && $this->get_options()['enabled'] == 1) { + public function is_enable($type): bool { + if (isset($this->get_options()[$type]) + && $this->get_options()[$type] == 1) { return true; } return false; } - public function is_wp_ver_disable(): bool { - if (isset($this->get_options()['wp_ver_disabled']) - && $this->get_options()['wp_ver_disabled'] == 1) { - return true; - } - - return false; - } - - public function is_asset_ver_disable(): bool { - if (isset($this->get_options()['asset_ver_disabled']) + public function is_disable($type): bool { + if (isset($this->get_options()[$type]) && $this->get_options()['asset_ver_disabled'] == 1) { return true; } @@ -91,50 +82,13 @@ if (!class_exists('MTEE')) { return false; } - public function is_noindex_nofollow_disable(): bool { - if (isset($this->get_options()['noindex_nofollow']) - && $this->get_options()['noindex_nofollow'] == '1') { - return true; - } - - return false; - } - - public function get_key_desc_status(): int { - if (isset($this->get_options()['enabled']) - && $this->get_options()['enabled'] == '1') { + public function get_key_setting($type): int { + if (isset($this->get_options()[$type]) + && $this->get_options()[$type] == '1') { return 1; } else { return 0; } } - - public function get_wp_ver_disabled(): int { - if (isset($this->get_options()['wp_ver_disabled']) - && $this->get_options()['wp_ver_disabled'] == '1') { - return 1; - } else { - return 0; - } - } - - public function get_asset_ver_disabled(): int { - if (isset($this->get_options()['asset_ver_disabled']) - && $this->get_options()['asset_ver_disabled'] == '1') { - return 1; - } else { - return 0; - } - } - - public function get_noindex_nofollow(): int { - if (isset($this->get_options()['noindex_nofollow']) - && $this->get_options()['noindex_nofollow'] == '1') { - return 1; - } else { - return 0; - } - } - } } diff --git a/config.php b/config.php index 029ff39..318a558 100644 --- a/config.php +++ b/config.php @@ -6,6 +6,7 @@ const MTEE_NAME_KEYWORDS = 'mtee_meta_keywords'; const MTEE_NAME_DESCRIPTION = 'mtee_meta_description'; const MTEE_NAME_NOINDEX = '_mtee_robots_noindex'; const MTEE_NAME_NOFOLLOW = '_mtee_robots_nofollow'; +const MTEE_CANONICAL_URL = 'mtee_canonical_url'; const MTEE_META_DESC_TOP_BASE = 'トップページです。'; const MTEE_META_DESC_SINGLE_BASE = 'ページです。'; diff --git a/css/mtee.css b/css/mtee.css index 658621c..64058fe 100644 --- a/css/mtee.css +++ b/css/mtee.css @@ -40,7 +40,6 @@ h2 { display: block; } - .mtee_box_border { border-top: 1px solid #ccc; } @@ -111,6 +110,12 @@ h2 { font-style: italic; } +.disabled_status { + display: block; + font-size: .8em; + color: #f35; +} + @media screen and (max-width: 768px) { .mtee_description_tmp_list { display: block; diff --git a/meta-tag-etc-extend.php b/meta-tag-etc-extend.php index 67894da..74bff28 100644 --- a/meta-tag-etc-extend.php +++ b/meta-tag-etc-extend.php @@ -8,6 +8,7 @@ Version: 1.0 /* Todo:OGPタグ +・canonical設定を追加する ・OGPタグを追加する <各OGP画像の保存> ・投稿のOGP用画像 => post_meta @@ -31,22 +32,30 @@ add_action('admin_enqueue_scripts', function () { $mtee = new MTEE; $options = $mtee->get_options(); -// 設定が有効の場合は、各フォームや出力用のクラスを有効にする -if ($mtee->is_enable()) { +// meta keywords, description設定有効の場合は、各フォームや出力用のクラスを有効にする +if ($mtee->is_enable('enabled')) { require_once MTEE_CLASS_DIR . 'mtee-tax-setting.php'; require_once MTEE_CLASS_DIR . 'mtee-post-setting.php'; - require_once MTEE_CLASS_DIR . 'mtee-output.php'; new mtee_tax_setting($options); new mtee_post_setting($options); - new mtee_meta_output; } -if ($mtee->is_noindex_nofollow_disable()) { +// canonical設定有効の場合は、フォームを有効にし既存のcanonicalを削除・表示 +if ($mtee->is_enable('canonical_setting')) { + remove_action('wp_head', 'rel_canonical'); + require_once MTEE_CLASS_DIR . 'mtee-canonical-setting.php'; + new mtee_canonical_setting($options); +} + +if ($mtee->is_disable('noindex_nofollow')) { require_once MTEE_CLASS_DIR . 'mtee-noindexnofollow-setting.php'; new mtee_noindexnofolow_setting($options); } -if ($mtee->is_wp_ver_disable() || $mtee->is_asset_ver_disable()) { +if ($mtee->is_disable('asset_ver_disabled') || $mtee->is_disable('asset_ver_disabled')) { require_once MTEE_CLASS_DIR . 'mtee-version-setting.php'; new mtee_version_setting($options); } + +require_once MTEE_CLASS_DIR . 'mtee-output.php'; +new mtee_meta_output; diff --git a/template/index.php b/template/index.php index 6de865a..a7de63c 100644 --- a/template/index.php +++ b/template/index.php @@ -1,10 +1,11 @@ get_key_desc_status(); -$wp_ver_disabled = $this->get_wp_ver_disabled(); -$asset_ver_disabled = $this->get_asset_ver_disabled(); -$noindex_nofollow = $this->get_noindex_nofollow(); +$enabled = $this->get_key_setting('enabled'); +$wp_ver_disabled = $this->get_key_setting('wp_ver_disabled'); +$asset_ver_disabled = $this->get_key_setting('asset_ver_disabled'); +$noindex_nofollow = $this->get_key_setting('noindex_nofollow'); +$canonical_setting = $this->get_key_setting('canonical_setting'); ?>個別設定していない投稿やカテゴリー等は、このテンプレートを適用します。
get_noindex_nofollow();個別設定していない投稿やカテゴリー等は、このテンプレートを適用します。
get_noindex_nofollow(); value="1">削除| meta keywords | +meta keywords + is_enable('enabled')): ?> + ※現在無効です + + |
キーワードはカンマ(,)区切りで入力してください |
|---|---|---|
| meta description | +meta description + is_enable('enabled')): ?> + ※現在無効です + + | |
| noindex nofollow | +noindex nofollow + is_enable('noindex_nofollow')): ?> + ※現在無効です + + | |
| Canonical URL + is_enable('canonical_setting')): ?> + ※現在無効です + | ++ | +
| meta keywords | +meta keywords + is_enable('enabled')): ?> + ※現在無効です + | @@ -277,7 +323,10 @@ $noindex_nofollow = $this->get_noindex_nofollow(); |
|---|---|---|
| meta description | +meta description + is_enable('enabled')): ?> + ※現在無効です + | get_noindex_nofollow(); |
| noindex nofollow | +noindex nofollow + is_enable('noindex_nofollow')): ?> + ※現在無効です + | |
| Canonical URL + is_enable('canonical_setting')): ?> + ※現在無効です + | ++ | +