From d7826ef2bec50e13f3d62d5e6474fbaa6a8404df Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 10 Jun 2021 20:38:46 +0900 Subject: [PATCH] =?UTF-8?q?WP=20PLUGIN=20MTEE(Meta=20Tag=20etc=20Extend)?= =?UTF-8?q?=20OGP=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ・OGP imageの設定 ・トップページとカスタム投稿アーカイブのOGP title、description追加 ・タイトルとディスクリプションの出力 --- class/mtee_meta_output_ogp.php | 135 ++++++++++++++++++--------------- meta-tag-etc-extend.php | 8 +- template/index.php | 50 +++++++++++- template/pages_meta.php | 58 ++++++++++++++ 4 files changed, 187 insertions(+), 64 deletions(-) create mode 100644 template/pages_meta.php diff --git a/class/mtee_meta_output_ogp.php b/class/mtee_meta_output_ogp.php index 03054d2..2336765 100644 --- a/class/mtee_meta_output_ogp.php +++ b/class/mtee_meta_output_ogp.php @@ -10,12 +10,12 @@ if (!class_exists('mtee_meta_output_ogp')) { use mtee_meta_desc_trait; - public $site_name; - public $ogp_type; - public $ogp_description; - public $ogp_title; - public $ogp_url; - public $ogp_img; + public $site_name = ''; + public $ogp_type = ''; + public $ogp_description = ''; + public $ogp_title = ''; + public $ogp_url = ''; + public $ogp_img = ''; public function __construct() { $this->site_name = get_bloginfo('name'); @@ -53,7 +53,9 @@ if (!class_exists('mtee_meta_output_ogp')) { $tags .= '' . PHP_EOL; $tags .= '' . PHP_EOL; $tags .= '' . PHP_EOL; -// $tags .= '' . PHP_EOL; + if (!empty($this->ogp_img)) { + $tags .= '' . PHP_EOL; + } $tags .= '' . PHP_EOL; // $tags .= '' . PHP_EOL; // $tags .= '' . PHP_EOL; @@ -80,6 +82,24 @@ if (!class_exists('mtee_meta_output_ogp')) { } public function create_ogp_title() { + global $post; + if (is_home() || is_front_page()) { + $this->ogp_title = get_option('_mtee')['top_page']['ogp_title']; + } elseif (is_post_type_archive()) { + $custom_post_name = get_post_type_object(get_post_type())->name; + $this->ogp_title = get_option('_mtee')['custom_post'][$custom_post_name]['ogp_title']; + } elseif (is_category() || is_tag() || is_tax()) { + $queried_object = get_queried_object(); + $this->ogp_title = get_term_meta($queried_object->term_id, MTEE_OGP_TITLE, true); + } elseif (is_page() || is_single()) { + $this->ogp_title = get_post_meta($post->ID, MTEE_OGP_TITLE, true); + } + if (empty($this->ogp_title)) { + $this->create_default_title(); + } + } + + public function create_default_title() { if (is_category()) { $this->ogp_title = single_cat_title('', false); } elseif (is_tag()) { @@ -96,11 +116,29 @@ if (!class_exists('mtee_meta_output_ogp')) { } public function create_ogp_description() { - $this->ogp_description = $this->site_name . MTEE_META_DESC_PARTICLE; - $this->ogp_description .= $this->set_ogp_description(); + $this->create_input_description(); + if (empty($this->ogp_description)) { + $this->set_default_description(); + } } - public function set_ogp_description() { + public function create_input_description() { + global $post; + if (is_home() || is_front_page()) { + $this->ogp_description = get_option('_mtee')['top_page']['ogp_description']; + } elseif (is_post_type_archive()) { + $custom_post_name = get_post_type_object(get_post_type())->name; + $this->ogp_description = get_option('_mtee')['custom_post'][$custom_post_name]['ogp_description']; + } elseif (is_category() || is_tag() || is_tax()) { + $queried_object = get_queried_object(); + $this->ogp_description = get_term_meta($queried_object->term_id, MTEE_OGP_DESC, true); + } elseif (is_page() || is_single()) { + $this->ogp_description = get_post_meta($post->ID, MTEE_OGP_DESC, true); + } + } + + public function set_default_description() { + $this->ogp_description = $this->site_name . MTEE_META_DESC_PARTICLE; $type = ''; $ogp_desc = ''; @@ -126,13 +164,12 @@ if (!class_exists('mtee_meta_output_ogp')) { } if (!empty($type)) { - $ogp_desc = $this->post_output_get_meta_desc($type); + $this->ogp_description .= $this->post_output_get_meta_desc($type); } if (empty($ogp_desc)) { - $ogp_desc = $this->create_default_description(); + $this->ogp_description .= $this->create_default_description(); } - return $ogp_desc; } @@ -160,81 +197,59 @@ if (!class_exists('mtee_meta_output_ogp')) { public function create_ogp_image() { global $post; - $id = ''; - $type = ''; if (is_post_type_archive()) { - $type = 'custom_post_archive'; + $this->set_custom_post_archive_ogp_img(); } elseif (is_category()) { - $id = get_query_var('cat'); - $type = 'cat'; + $this->set_category_ogp_img(get_query_var('cat')); } elseif (is_tag()) { - $id = get_query_var('tag_id'); - $type = 'tag'; + $this->set_tag_ogp_img(get_query_var('tag_id')); } elseif (is_tax()) { - $id = get_queried_object()->term_id; - $type = 'tax'; + $this->set_tax_ogp_img(get_queried_object()->term_id); } elseif (is_home() || is_front_page()) { - $type = 'top_page'; + $this->set_top_page_ogp_img(); } elseif (is_page() || is_single()) { - $id = $post->ID; - $type = 'posts'; - } - return $this->get_ogp_tag_img($id, $type); - } - - public function get_ogp_tag_img($id, $type) { - if ($type == 'top_page') { - $canonical = $this->set_top_page_ogp_img(); - } elseif ($type == 'custom_post_archive') { - $canonical = $this->set_custom_post_archive_ogp_img(); - } elseif ($type == 'posts') { - $canonical = $this->set_post_ogp_img($id); - } elseif ($type == 'cat') { - $canonical = $this->set_category_ogp_img($id); - } elseif ($type == 'tag') { - $canonical = $this->set_tag_ogp_img($id); - } elseif ($type == 'tax') { - $canonical = $this->set_tax_ogp_img($id); + $this->set_post_ogp_img($post->ID); } } public function set_top_page_ogp_img() { - $img = get_option('_mtee')['top_page']['ogp_img']; - if (empty($img)) { - $img = ''; - } - return $img; + $id = get_option('_mtee')['top_page']['ogp_img']; + $this->get_thumbnail($id); } public function set_custom_post_archive_ogp_img() { $custom_post_name = get_post_type_object(get_post_type())->name; - $img = get_option('_mtee')['custom_post'][$custom_post_name]['ogp_img']; - if (empty($img)) { - $img = ''; - } - return $img; + $id = get_option('_mtee')['custom_post'][$custom_post_name]['ogp_img']; + $this->get_thumbnail($id); } public function set_post_ogp_img($id) { - $img = get_post_meta($id, MTEE_OGP_IMG, true); - + $id = get_post_meta($id, MTEE_OGP_IMG, true); + $this->get_thumbnail($id); } public function set_category_ogp_img($id) { - $img = get_term_meta($id, MTEE_OGP_IMG, true); - + $id = get_term_meta($id, MTEE_OGP_IMG, true); + $this->get_thumbnail($id); } public function set_tag_ogp_img($id) { - $img = get_term_meta($id, MTEE_OGP_IMG, true); - + $id = get_term_meta($id, MTEE_OGP_IMG, true); + $this->get_thumbnail($id); } public function set_tax_ogp_img($id) { - $img = get_term_meta($id, MTEE_OGP_IMG, true); - + $id = get_term_meta($id, MTEE_OGP_IMG, true); + $this->get_thumbnail($id); } + public function get_thumbnail($id) { + if (!empty($id)) { + $this->ogp_img = wp_get_attachment_url($id); + } elseif (has_post_thumbnail($id)) { + $this->ogp_img = wp_get_attachment_url(get_post_thumbnail_id($id)); + } + } } } \ No newline at end of file diff --git a/meta-tag-etc-extend.php b/meta-tag-etc-extend.php index e544f29..a6ea290 100644 --- a/meta-tag-etc-extend.php +++ b/meta-tag-etc-extend.php @@ -8,7 +8,13 @@ Version: 1.0 /* Todo:OGPタグ -タグの出力 +画像出力 + +facebook + + +Twitter + */ include_once 'config.php'; diff --git a/template/index.php b/template/index.php index 2a46594..3345873 100644 --- a/template/index.php +++ b/template/index.php @@ -207,6 +207,8 @@ $rss_disabled = $this->get_key_setting('rss_disabled'); $noindex = $opt['top_page']['noindex'] ?? '0'; $nofollow = $opt['top_page']['nofollow'] ?? '0'; $canonical_url = $opt['top_page']['canonical'] ?? ''; + $ogp_title = $opt['top_page']['ogp_title'] ?? ''; + $ogp_description = $opt['top_page']['ogp_description'] ?? ''; $ogp_img = $opt['top_page']['ogp_img'] ?? ''; ?>
@@ -218,7 +220,7 @@ $rss_disabled = $this->get_key_setting('rss_disabled'); + value="">
キーワードはカンマ(,)区切りで入力してください @@ -269,6 +271,26 @@ $rss_disabled = $this->get_key_setting('rss_disabled'); placeholder=""> + + OGP title + is_enable('enabled')): ?> + ※現在無効です + + + + + + + OGP description + is_enable('enabled')): ?> + ※現在無効です + + + + + OGP Image @@ -289,8 +311,10 @@ $rss_disabled = $this->get_key_setting('rss_disabled'); $description = $opt['custom_post'][$custom_post]['description'] ?? ''; $noindex = $opt['custom_post'][$custom_post]['noindex'] ?? '0'; $nofollow = $opt['custom_post'][$custom_post]['nofollow'] ?? '0'; - $canonical_url = $opt['custom_post'][$custom_post]['canonical'] ?? '0'; - $ogp_img = $opt['custom_post'][$custom_post]['ogp_img'] ?? '0'; + $canonical_url = $opt['custom_post'][$custom_post]['canonical'] ?? ''; + $ogp_title = $opt['custom_post'][$custom_post]['ogp_title'] ?? ''; + $ogp_description = $opt['custom_post'][$custom_post]['ogp_description'] ?? ''; + $ogp_img = $opt['custom_post'][$custom_post]['ogp_img'] ?? ''; ?>
@@ -360,6 +384,26 @@ $rss_disabled = $this->get_key_setting('rss_disabled'); placeholder=""> + + OGP keywords + is_enable('enabled')): ?> + ※現在無効です + + + + + + OGP description + is_enable('enabled')): ?> + ※現在無効です + + + + OGP Image diff --git a/template/pages_meta.php b/template/pages_meta.php new file mode 100644 index 0000000..a7312cb --- /dev/null +++ b/template/pages_meta.php @@ -0,0 +1,58 @@ +

トップページ

+ +
+ + + + + + + + + +
meta keywords +
キーワードはカンマ(,)区切りで入力してください +
meta description +
+
+ 0): ?> +

カスタム投稿アーカイブ

+ + + +
+

+ label; ?> + () +

+ + + + + + + + + +
meta keywords +
キーワードはカンマ(,)区切りで入力してください +
meta description +
+
+ + +