Compare commits

...

2 Commits

Author SHA1 Message Date
nobu 03ddba7a9b Merge pull request 'WP PLUGIN MTEE(Meta Tag etc Extend) キーワードのテンプレート設定追加' (#7) from 機能追加_キーワードの雛形設定 into master
Reviewed-on: https://develop.n-k-y.net/repo/WP_PLUGIN/Meta_Tag_etc_Extend/pulls/7
2021-05-25 15:08:17 +09:00
nobu 1cc61a0c24 WP PLUGIN MTEE(Meta Tag etc Extend) キーワードのテンプレート設定追加
・meta keywords未設定の投稿、カテゴリー、タグ、カスタム分類のデフォルト表示テンプレート設定の追加
2021-05-25 15:07:29 +09:00
4 changed files with 176 additions and 45 deletions
+116 -18
View File
@@ -100,41 +100,139 @@ if (!class_exists('mtee_meta_output')) {
}
public function create_default_keywords_description(): array {
$keywords[] = $this->site_name; //サイトタイトル
if (!empty(get_bloginfo('description'))) {
$keywords[] = get_bloginfo('description'); //サイトタイトル
}
$this->createKeywords($keywords);
$this->createDescription();
$keywords = $this->create_default_keywords();
$this->create_default_description();
$meta_data = array(
MTEE_NAME_KEYWORDS => implode(',', array_reverse($keywords)),
MTEE_NAME_KEYWORDS => implode(',', $keywords),
MTEE_NAME_DESCRIPTION => $this->description,
);
return $this->create_meta_tags($meta_data);
}
private function createKeywords(&$keywords) {
if (is_category() || is_tag()) {
$keywords[] = single_cat_title('', false);
private function create_default_keywords() {
if (is_category()) {
$type = 'category';
$title = single_cat_title('', false);
} elseif (is_tag()) {
$type = 'tag';
$title = single_cat_title('', false);
} elseif (is_tax()) {
$keywords[] = single_term_title('', false);
$type = 'tax';
$title = single_term_title('', false);
} elseif (is_post_type_archive()) {
$keywords[] = post_type_archive_title('', false);
$type = 'custom_post_archive';
$title = post_type_archive_title('', false);
} elseif (is_single()) {
$type = 'post';
$title = get_the_title();
} elseif (is_page()) {
$type = 'page';
$title = get_the_title();
}
$custom_keywords_temp = get_option('_mtee')['keywords_tmp'];
if (empty($custom_keywords_temp[$type])) {
return $this->get_keywords_data($type, $title);
}
return $this->get_custom_template_keywords_data($custom_keywords_temp, $type, $title);
}
private function get_keywords_data($type, $keyword) {
$keywords = array();
$keywords[] = $this->site_name; //サイトタイトル
if (!empty(get_bloginfo('description'))) {
$keywords[] = get_bloginfo('description'); //サイトタイトル
}
if ($type == 'post') {
$this->get_single_keywords_tax($keywords);
} else {
$keywords[] = get_the_title();
$keywords[] = $keyword;
}
return array_reverse($keywords);
}
private function get_custom_template_keywords_data($custom_keywords_temp, $type, $title) {
$targets = explode(',', $custom_keywords_temp[$type]);
$keywords = array();
foreach ($targets as $target) {
switch ($target) {
case '##title##':
$keywords[] = $title;
break;
case '##site_name##':
$keywords[] = $this->site_name;
break;
case '##site_description##':
$keywords[] = get_bloginfo('description');
break;
case '##category##':
$this->get_custom_template_single_keywords($keywords, 'category');
break;
case '##tag##':
$this->get_custom_template_single_keywords($keywords, 'post_tag');
break;
case '##custom_tax##':
$this->get_custom_template_single_keywords($keywords, 'custom_tax');
break;
}
}
return $keywords;
}
private function get_custom_template_single_keywords(&$keywords, $type) {
global $post;
$taxonomies = $this->get_single_keyword_tax_data();
if ($type == 'custom_tax') {
foreach ($taxonomies as $target) {
if ($target != 'post_format' && $target != 'category' && $target != 'post_tag') {
$this->get_custom_keywords_terms($keywords, $post->ID, $target);
}
}
} else {
foreach ($taxonomies as $target) {
if ($target == $type) {
$this->get_custom_keywords_terms($keywords, $post->ID, $target);
}
}
}
}
private function createDescription() {
private function get_single_keywords_tax(&$keywords) {
global $post;
$taxonomies = $this->get_single_keyword_tax_data();
foreach ($taxonomies as $target) {
if ($target != 'post_format') {
$this->get_custom_keywords_terms($keywords, $post->ID, $target);
}
}
}
private function get_single_keyword_tax_data(): array {
global $post;
return array_reverse(get_object_taxonomies(get_post_type($post->ID)));
}
private function get_custom_keywords_terms(&$keywords, $id, $target) {
$terms = get_the_terms($id, $target);
if ($terms) {
foreach ($terms as $term) {
$keywords[] = $term->name;
}
}
}
private function create_default_description() {
$type = '';
$title = '';
$suffix_text = MTEE_META_DESC_ARCHIVE_BASE;
if (is_home() || is_front_page()) { //ページ
$this->description .= MTEE_META_DESC_TOP_BASE;
return;
} elseif (is_page() || is_single()) { //ページ
$type = 'page';
$title = get_the_title();
@@ -158,7 +256,7 @@ if (!class_exists('mtee_meta_output')) {
if (isset($replacement['org'])) {
$search = array('<###>', '<%%%>');
$description_noun = MTEE_META_DESC_BEFORE_BRACKETS . '<###>' . MTEE_META_DESC_AFTER_BRACKETS . '<%%%>';
$this->description .= str_replace($search, $replacement, $description_noun);
$this->description .= str_replace($search, $replacement['org'], $description_noun);
}
}
@@ -169,8 +267,8 @@ if (!class_exists('mtee_meta_output')) {
$template = get_option('_mtee')['description_tmp'];
if (isset($template[$type]) && !empty($template[$type])) {
$search = array('##title##', '##site_name##');
$replacement = array($title, get_bloginfo('name'));
$search = array('##title##', '##site_name##', '##site_description##');
$replacement = array($title, get_bloginfo('name'), get_bloginfo('description'));
$this->description = str_replace($search, $replacement, $template[$type]);
} else {
return array('org' => array($title, $suffix_text));
+2 -14
View File
@@ -63,19 +63,6 @@ h2 {
margin: 0
}
.mtee-example {
width: 95%;
margin-bottom: 2em;
padding: 1em;
background: #f9f9f9;
border: 1px solid #666;
border-radius: 6px;
}
.mtee-example h4 {
margin: 0 0 .5em;
}
.meta_noindex_nofollow_box {
display: flex;
margin-bottom: 2em;
@@ -114,7 +101,8 @@ h2 {
margin-left: 0;
}
.mtee_description_tmp {
.mtee_description_tmp,
.mtee_keywords_tmp {
width: 100%;
}
-3
View File
@@ -7,9 +7,6 @@ Version: 1.0
*/
/*
Todo:meta keywords & description
・キーワードの雛形設定
Todo:OGPタグ
・OGPタグを追加する
<各OGP画像の保存>
+58 -10
View File
@@ -76,18 +76,65 @@ $noindex_nofollow = $this->get_noindex_nofollow();
<?php endif; ?>
</table>
<?php endif; ?>
<aside class="mtee-example">
<h4>keywordsの初期表示</h4>
<div class="mtee-form-box mtee_box_border">
<h3>meta keywords テンプレート設定</h3>
<p>個別設定していない投稿やカテゴリー等は、このテンプレートを適用します。</p>
<?php
$post_meta_keywords_tmp = get_option('_mtee')['keywords_tmp']['post'] ?? '';
$page_meta_keywords_tmp = get_option('_mtee')['keywords_tmp']['page'] ?? '';
$cat_meta_keywords_tmp = get_option('_mtee')['keywords_tmp']['category'] ?? '';
$tag_meta_keywords_tmp = get_option('_mtee')['keywords_tmp']['tag'] ?? '';
$tax_meta_keywords_tmp = get_option('_mtee')['keywords_tmp']['tax'] ?? '';
?>
<dl class="mtee_description_tmp_list">
<dt>投稿</dt>
<dd>
<input class="mtee_keywords_tmp" type="text" name="_mtee[keywords_tmp][post]"
value="<?php echo $post_meta_keywords_tmp; ?>"
placeholder="##title##,##category##,##tag##,##custom_tax##,##description##,##site_name##"
</dd>
<dt>ページ</dt>
<dd>
<input class="mtee_keywords_tmp" type="text" name="_mtee[keywords_tmp][page]"
value="<?php echo $page_meta_keywords_tmp; ?>"
placeholder="##title##,##description##,##site_name##"
</dd>
<dt>カテゴリー</dt>
<dd>
<input class="mtee_keywords_tmp" type="text" name="_mtee[keywords_tmp][category]"
value="<?php echo $cat_meta_keywords_tmp; ?>"
placeholder="##title##,##description##,##site_name##">
</dd>
<dt>タグ</dt>
<dd>
<input class="mtee_keywords_tmp" type="text" name="_mtee[keywords_tmp][tag]"
value="<?php echo $tag_meta_keywords_tmp; ?>"
placeholder="##title##,##description##,##site_name##">
</dd>
<dt>カスタム分類</dt>
<dd>
<input class="mtee_keywords_tmp" type="text" name="_mtee[keywords_tmp][tax]"
value="<?php echo $tax_meta_keywords_tmp; ?>"
placeholder="##title##,##description##,##site_name##">
</dd>
</dl>
<ul>
<li>[各ページ、カテゴリー、タグ等のタイトル],<?php
if (!empty(get_bloginfo('description'))) {
echo get_bloginfo('description') . ',';
}
echo get_bloginfo('name');
?>
<li><b>共通</b>
<ul>
<li>##title##:投稿、ページ、カテゴリー等のタイトル</li>
<li>##site_name##:サイト名(<?php echo get_bloginfo('name'); ?></li>
<li>##site_description##:サイトの説明(<?php echo get_bloginfo('description'); ?></li>
</ul>
</li>
<li><b>投稿</b>
<ul>
<li>##category##:投稿のカテゴリー</li>
<li>##tag##:投稿のタグ</li>
<li>##custom_tax##:投稿のカスタム分類</li>
</ul>
</li>
</ul>
</aside>
</div>
<div class="mtee-form-box mtee_box_border">
<h3>meta description テンプレート設定</h3>
<p>個別設定していない投稿やカテゴリー等は、このテンプレートを適用します。</p>
@@ -125,7 +172,8 @@ $noindex_nofollow = $this->get_noindex_nofollow();
</dl>
<ul>
<li>##site_name##:サイト名(<?php echo get_bloginfo('name'); ?></li>
<li>##title##:出力対象(投稿、ページ、カテゴリー等)毎のタイトル</li>
<li>##site_description##:サイトの説明(<?php echo get_bloginfo('description'); ?></li>
<li>##title##:投稿、ページ、カテゴリー等のタイトル</li>
</ul>
</div>
<div class="mtee-form-box mtee_box_border">