WP PLUGIN MTEE(Meta Tag etc Extend) カテゴリー、タグ、カスタム分類のcanonical設定追加

・カテゴリー、タグ、カスタム分類にcanonical urlの入力設定メタボックス追加
・デフォルトのcanonical urlを追加
This commit is contained in:
2021-05-27 16:10:58 +09:00
parent 0870f12825
commit ee28571645
3 changed files with 156 additions and 5 deletions
+66 -2
View File
@@ -12,15 +12,26 @@ if (!class_exists('mtee_canonical_setting')) {
class mtee_canonical_setting {
private $options;
private $tax_place_holder;
public function __construct($options) {
$this->options = $options;
if ($this->options['enabled'] == 1) {
if ($this->options['canonical_setting'] == 1) {
//Posts
add_action('admin_menu', array($this, 'add_meta_fields'));
add_action('save_post', array($this, 'save_meta_fields'));
//Taxonomy
$this->set_tax_meta_box(); //各投稿画面
add_action('create_term', array($this, 'save_terms')); //新規追加用フック
add_action('edit_terms', array($this, 'save_terms')); //編集ページ用フック
}
}
//-------------------------------------------------------------------------------------------
// 投稿、ページのカスタムフィールド設定
//-------------------------------------------------------------------------------------------
public function add_meta_fields() {
//add_meta_box(表示される入力ボックスのHTMLのID, ラベル, 表示する内容を作成する関数名, 投稿タイプ, 表示方法)
$default_built_ins = array(
@@ -57,7 +68,7 @@ if (!class_exists('mtee_canonical_setting')) {
echo '<div class="meta_key_desc_box">
<label style="font-weight: bold; display: block">Canonical URL</label>
<div>
<input style="width:100%" type="text" name="' . MTEE_CANONICAL_URL . '" value="' . get_post_meta($post->ID, MTEE_CANONICAL_URL, true) . '" />
<input style="width:100%" type="text" name="' . MTEE_CANONICAL_URL . '" value="' . get_post_meta($post->ID, MTEE_CANONICAL_URL, true) . '"/>
</div>
</div>';
}
@@ -70,6 +81,59 @@ if (!class_exists('mtee_canonical_setting')) {
delete_post_meta($post_id, MTEE_CANONICAL_URL);
}
}
//-------------------------------------------------------------------------------------------
// タクソノミーのカスタムフィールド設定
//-------------------------------------------------------------------------------------------
public function set_tax_meta_box() {
$taxs = $this->set_tax_types();
foreach ($taxs as $tax) {
add_action($tax . '_add_form_fields', array($this, 'add_tax_term_fields'));
add_action($tax . '_edit_form_fields', array($this, 'edit_tax_term_fields'));
}
}
public function set_tax_types(): array {
$custom_tax = get_taxonomies(array('public' => true, '_builtin' => false));
$tax_array = array_merge(array('category', 'post_tag'), $custom_tax);
$add_tax = filter_input(INPUT_GET, 'taxonomy');
if (!empty($add_tax)) {
return array_merge($tax_array, array($add_tax));
}
return $tax_array;
}
public function add_tax_term_fields($tag) {
$form = '<div class="form-field">Canonical URL<br>##CONTENTS##</div>';
echo $this->insert_term_meta_fields($tag, $form);
}
public function edit_tax_term_fields($tag) {
$form = '<tr class="form-field">
<th scope="row">Canonical URL</th>
<td>##CONTENTS##</td>
</tr>';
echo $this->insert_term_meta_fields($tag, $form);
}
public function insert_term_meta_fields($tag, $form) {
$canonical_url = '';
if (isset($tag->term_id)) {
$canonical_url = get_term_meta($tag->term_id, MTEE_CANONICAL_URL, true);
}
$add_meta = '<input type="text" name="' . MTEE_CANONICAL_URL . '" value="' . $canonical_url . '"/>';
return str_replace('##CONTENTS##', $add_meta, $form);
}
public function save_terms($term_id) {
if (array_key_exists(MTEE_CANONICAL_URL, $_POST)) {
update_term_meta($term_id, MTEE_CANONICAL_URL, $_POST[MTEE_CANONICAL_URL]);
}
}
}
}
+3 -3
View File
@@ -30,7 +30,7 @@ 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'));
add_action('wp_head', array($this, 'set_canonical_url'));
}
//--------------------------------------------------------------------
@@ -185,10 +185,10 @@ if (!class_exists('mtee_meta_output')) {
$type = 'custom_post_archive';
} elseif (is_category()) {
$id = get_query_var('cat');
$type = 'tax';
$type = 'cat';
} elseif (is_tag()) {
$id = get_query_var('tag_id');
$type = 'tax';
$type = 'tag';
} elseif (is_tax()) {
$id = get_queried_object()->term_id;
$type = 'tax';
+87
View File
@@ -0,0 +1,87 @@
<?php
if (!defined('ABSPATH')) {
exit;
} // Exit if accessed directly
if (!trait_exists('output_canonical')) {
trait output_canonical {
public function get_canonical_data($id, $type) {
if (empty($type)) {
return;
}
$canonical = '';
if ($type == 'top_page') {
$canonical = $this->set_top_page_canonical();
} elseif ($type == 'custom_post_archive') {
$canonical = $this->set_custom_post_archive_canonical();
} elseif ($type == 'posts') {
$canonical = $this->set_post_canonical($id);
} elseif ($type == 'cat') {
$canonical = $this->set_category_canonical($id);
} elseif ($type == 'tag') {
$canonical = $this->set_tag_canonical($id);
} elseif ($type == 'tax') {
$canonical = $this->set_tax_canonical($id);
}
if ($canonical) {
return '<link rel="canonical" href="' . $canonical . '">' . PHP_EOL;
}
}
public function set_top_page_canonical() {
$canonical = get_option('_mtee')['top_page']['canonical'];
if (empty($canonical)) {
$canonical = get_bloginfo('url');
}
return $canonical;
}
public function set_custom_post_archive_canonical() {
$custom_post_name = get_post_type_object(get_post_type())->name;
$canonical = get_option('_mtee')['custom_post'][$custom_post_name]['canonical'];
if (empty($canonical)) {
$canonical = get_post_type_archive_link($custom_post_name);
}
return $canonical;
}
public function set_post_canonical($id) {
$canonical = get_post_meta($id, MTEE_CANONICAL_URL, true);
if (empty($canonical)) {
$canonical = get_permalink($id);
global $page, $paged;
if ($paged >= 2 || $page >= 2) {
$canonical .= 'page/' . max($paged, $page) . '/';
}
}
return $canonical;
}
public function set_category_canonical($id) {
$canonical = get_term_meta($id, MTEE_CANONICAL_URL, true);
if (empty($canonical)) {
$canonical = get_category_link($id);
}
return $canonical;
}
public function set_tag_canonical($id) {
$canonical = get_term_meta($id, MTEE_CANONICAL_URL, true);
if (empty($canonical)) {
$canonical = get_tag_link($id);
}
return $canonical;
}
public function set_tax_canonical($id) {
$canonical = get_term_meta($id, MTEE_CANONICAL_URL, true);
if (empty($canonical)) {
$canonical = get_term_link($id);
}
return $canonical;
}
}
}