WP PLUGIN MTEE(Meta Tag etc Extend) キーワード、カノニカルのplaceholder設定
・各投稿、各タームでキーワード、ディスクリプション、カノニカルのデフォルト設定値をplaceholderに埋め込み
This commit is contained in:
@@ -55,7 +55,9 @@ 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) . '"
|
||||
placeholder="' . $this->set_default_post_canonical_url($post->ID) . '" />
|
||||
</div>
|
||||
</div>';
|
||||
}
|
||||
@@ -69,6 +71,15 @@ if (!class_exists('mtee_canonical_setting')) {
|
||||
}
|
||||
}
|
||||
|
||||
public function set_default_post_canonical_url($id) {
|
||||
$canonical = get_permalink($id);
|
||||
global $page, $paged;
|
||||
if ($paged >= 2 || $page >= 2) {
|
||||
$canonical .= 'page/' . max($paged, $page) . '/';
|
||||
}
|
||||
return $canonical;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------
|
||||
// タクソノミーのカスタムフィールド設定
|
||||
//-------------------------------------------------------------------------------------------
|
||||
@@ -108,13 +119,29 @@ if (!class_exists('mtee_canonical_setting')) {
|
||||
|
||||
public function insert_term_meta_fields($tag, $form) {
|
||||
$canonical_url = '';
|
||||
$placeholder = '';
|
||||
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 . '"/>';
|
||||
if (isset($tag->term_id) && empty($canonical_url)) {
|
||||
$placeholder = $this->set_default_tax_canonical_url($tag->term_id, $tag->taxonomy);
|
||||
}
|
||||
$add_meta = '<input type="text" name="' . MTEE_CANONICAL_URL . '" value="' . $canonical_url . '"
|
||||
placeholder="' . $placeholder . '" />';
|
||||
return str_replace('##CONTENTS##', $add_meta, $form);
|
||||
}
|
||||
|
||||
public function set_default_tax_canonical_url($id, $taxonomy) {
|
||||
if ($taxonomy == 'category') {
|
||||
$placeholder = get_category_link($id);
|
||||
} elseif ($taxonomy == 'post_tag') {
|
||||
$placeholder = get_tag_link($id);
|
||||
} else {
|
||||
$placeholder = get_term_link((int)$id);
|
||||
}
|
||||
return $placeholder;
|
||||
}
|
||||
|
||||
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]);
|
||||
|
||||
@@ -115,7 +115,7 @@ if (!class_exists('mtee_meta_output_canonical')) {
|
||||
public function set_tax_canonical($id) {
|
||||
$canonical = get_term_meta($id, MTEE_CANONICAL_URL, true);
|
||||
if (empty($canonical)) {
|
||||
$canonical = get_term_link($id);
|
||||
$canonical = get_term_link((int)$id);
|
||||
}
|
||||
return $canonical;
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ if (!class_exists('mtee_meta_output_keydesc')) {
|
||||
}
|
||||
}
|
||||
|
||||
public function create_default_keywords() {
|
||||
public function create_default_keywords(): array {
|
||||
$type = '';
|
||||
$title = '';
|
||||
if (is_category()) {
|
||||
@@ -158,7 +158,7 @@ if (!class_exists('mtee_meta_output_keydesc')) {
|
||||
|
||||
}
|
||||
|
||||
public function get_keywords_data($type, $keyword) {
|
||||
public function get_keywords_data($type, $keyword): array {
|
||||
$keywords = array();
|
||||
$keywords[] = $this->site_name; //サイトタイトル
|
||||
if (!empty(get_bloginfo('description'))) {
|
||||
@@ -172,7 +172,7 @@ if (!class_exists('mtee_meta_output_keydesc')) {
|
||||
return array_reverse($keywords);
|
||||
}
|
||||
|
||||
public function get_custom_template_keywords_data($custom_keywords_temp, $type, $title) {
|
||||
public function get_custom_template_keywords_data($custom_keywords_temp, $type, $title): array {
|
||||
$targets = explode(',', $custom_keywords_temp[$type]);
|
||||
$keywords = array();
|
||||
foreach ($targets as $target) {
|
||||
|
||||
+70
-39
@@ -1,29 +1,28 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
} // Exit if accessed directly
|
||||
|
||||
if ( ! class_exists( 'mtee_post_setting' ) ) {
|
||||
if (!class_exists('mtee_post_setting')) {
|
||||
|
||||
/**
|
||||
* Class mtee_post_setting
|
||||
*/
|
||||
class mtee_post_setting {
|
||||
class mtee_post_setting {
|
||||
|
||||
private $options;
|
||||
private $options;
|
||||
|
||||
public function __construct( $options ) {
|
||||
$this->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 __construct($options) {
|
||||
$this->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, ラベル, 表示する内容を作成する関数名, 投稿タイプ, 表示方法)
|
||||
$custom_posts = array_values(get_post_types(array('public' => true, '_builtin' => false)));
|
||||
$target_posts = array_merge(array('page', 'post',), $custom_posts);
|
||||
public function add_meta_fields() {
|
||||
//add_meta_box(表示される入力ボックスのHTMLのID, ラベル, 表示する内容を作成する関数名, 投稿タイプ, 表示方法)
|
||||
$target_posts = $this->get_all_post_type();
|
||||
foreach ($target_posts as $target_post) {
|
||||
add_meta_box(
|
||||
'meta_setting',
|
||||
@@ -35,34 +34,66 @@ if ( ! class_exists( 'mtee_post_setting' ) ) {
|
||||
'normal'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// カスタムフィールドの入力エリア
|
||||
public function insert_meta_fields() {
|
||||
global $post;
|
||||
echo '<div class="meta_key_desc_box">
|
||||
public function get_all_post_type(): array {
|
||||
$custom_posts = array_values(get_post_types(array('public' => true, '_builtin' => false)));
|
||||
return array_merge(array('page', 'post',), $custom_posts);
|
||||
}
|
||||
|
||||
// カスタムフィールドの入力エリア
|
||||
public function insert_meta_fields() {
|
||||
global $post;
|
||||
echo '<div class="meta_key_desc_box">
|
||||
<label style="font-weight: bold; display: block">meta keywords</label>
|
||||
<div><input style="width:100%" type="text" name="' . MTEE_NAME_KEYWORDS . '" value="' . get_post_meta( $post->ID, MTEE_NAME_KEYWORDS, true ) . '" />
|
||||
<div><input style="width:100%" type="text" name="' . MTEE_NAME_KEYWORDS . '"
|
||||
value="' . get_post_meta($post->ID, MTEE_NAME_KEYWORDS, true) . '"
|
||||
placeholder="' . $this->set_default_keywords() . '"/>
|
||||
</div>';
|
||||
echo '<label style="font-weight: bold; display: block">meta description</label>
|
||||
<div><input style="width:100%" type="text" name="' . MTEE_NAME_DESCRIPTION . '" value="' . get_post_meta( $post->ID, MTEE_NAME_DESCRIPTION, true ) . '" />
|
||||
echo '<label style="font-weight: bold; display: block">meta description</label>
|
||||
<div><input style="width:100%" type="text" name="' . MTEE_NAME_DESCRIPTION . '"
|
||||
value="' . get_post_meta($post->ID, MTEE_NAME_DESCRIPTION, true) . '"
|
||||
placeholder="' . $this->set_default_description() . '"/>
|
||||
</div>
|
||||
</div>';
|
||||
}
|
||||
}
|
||||
|
||||
// カスタムフィールドの値を保存
|
||||
public function save_meta_fields( $post_id ) {
|
||||
if ( ! empty( $_POST[ MTEE_NAME_KEYWORDS ] ) ) { //meta_keywordsが入力されている場合
|
||||
update_post_meta( $post_id, MTEE_NAME_KEYWORDS, $_POST[ MTEE_NAME_KEYWORDS ] ); //値を保存
|
||||
} else { //未入力の場合は値を削除
|
||||
delete_post_meta( $post_id, MTEE_NAME_KEYWORDS );
|
||||
}
|
||||
if ( ! empty( $_POST[ MTEE_NAME_DESCRIPTION ] ) ) {
|
||||
update_post_meta( $post_id, MTEE_NAME_DESCRIPTION, $_POST[ MTEE_NAME_DESCRIPTION ] );
|
||||
} else {
|
||||
delete_post_meta( $post_id, MTEE_NAME_DESCRIPTION );
|
||||
}
|
||||
}
|
||||
}
|
||||
// カスタムフィールドの値を保存
|
||||
public function save_meta_fields($post_id) {
|
||||
if (!empty($_POST[MTEE_NAME_KEYWORDS])) { //meta_keywordsが入力されている場合
|
||||
update_post_meta($post_id, MTEE_NAME_KEYWORDS, $_POST[MTEE_NAME_KEYWORDS]); //値を保存
|
||||
} else { //未入力の場合は値を削除
|
||||
delete_post_meta($post_id, MTEE_NAME_KEYWORDS);
|
||||
}
|
||||
if (!empty($_POST[MTEE_NAME_DESCRIPTION])) {
|
||||
update_post_meta($post_id, MTEE_NAME_DESCRIPTION, $_POST[MTEE_NAME_DESCRIPTION]);
|
||||
} else {
|
||||
delete_post_meta($post_id, MTEE_NAME_DESCRIPTION);
|
||||
}
|
||||
}
|
||||
|
||||
public function set_default_keywords() {
|
||||
global $post_type;
|
||||
if ($post_type == 'page') {
|
||||
if (!empty($this->options['keywords_tmp']['page'])) {
|
||||
return $this->options['keywords_tmp']['page'];
|
||||
}
|
||||
return '##title##,##description##,##site_name##';
|
||||
} else {
|
||||
if (!empty($this->options['keywords_tmp']['post'])) {
|
||||
return $this->options['keywords_tmp']['post'];
|
||||
}
|
||||
return '##title##,##category##,##tag##,##custom_tax##,##description##,##site_name##';
|
||||
}
|
||||
}
|
||||
|
||||
public function set_default_description() {
|
||||
if (!empty($this->options['description_tmp']['page'])) {
|
||||
return $this->options['description_tmp']['page'];
|
||||
}
|
||||
return '##site_name##の' . MTEE_META_DESC_BEFORE_BRACKETS . '##title##' . MTEE_META_DESC_AFTER_BRACKETS . MTEE_META_DESC_SINGLE_BASE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+64
-16
@@ -39,43 +39,91 @@ if (!class_exists('mtee_tax_setting')) {
|
||||
}
|
||||
|
||||
function insert_term_meta_add_fields($tag) {
|
||||
$key_value = '';
|
||||
$desc_value = '';
|
||||
if (isset($tag->term_id)) {
|
||||
$key_value = get_term_meta($tag->term_id, MTEE_NAME_KEYWORDS, true);
|
||||
$desc_value = get_term_meta($tag->term_id, MTEE_MTEE_NAME_DESCRIPTION, true);
|
||||
}
|
||||
$input_values = $this->set_input_values($tag);
|
||||
echo '
|
||||
<div class="form-field">
|
||||
meta keywords<br>
|
||||
<input type="text" name="' . MTEE_NAME_KEYWORDS . '" value="' . $key_value . '" class="tax-meta-field" />
|
||||
<input type="text" name="' . MTEE_NAME_KEYWORDS . '"
|
||||
value="' . $input_values['keywords'] . '"
|
||||
placeholder="' . $input_values['keyword_pl'] . '"
|
||||
class="tax-meta-field" />
|
||||
</div>
|
||||
<div class="form-field">
|
||||
meta description<br>
|
||||
<input type="text" name="' . MTEE_NAME_DESCRIPTION . '" value="' . $desc_value . '" class="tax-meta-field" />
|
||||
<input type="text" name="' . MTEE_NAME_DESCRIPTION . '"
|
||||
value="' . $input_values['desc'] . '"
|
||||
placeholder="' . $input_values['desc_pl'] . '"
|
||||
class="tax-meta-field" />
|
||||
</div>
|
||||
';
|
||||
}
|
||||
|
||||
function insert_term_meta_edit_fields($tag) {
|
||||
$key_value = '';
|
||||
$desc_value = '';
|
||||
if (isset($tag->term_id)) {
|
||||
$key_value = get_term_meta($tag->term_id, MTEE_NAME_KEYWORDS, true);
|
||||
$desc_value = get_term_meta($tag->term_id, MTEE_NAME_DESCRIPTION, true);
|
||||
}
|
||||
$input_values = $this->set_input_values($tag);
|
||||
echo '
|
||||
<tr class="form-field">
|
||||
<th scope="row">meta keywords</th>
|
||||
<td><input type="text" name="' . MTEE_NAME_KEYWORDS . '" value="' . $key_value . '" class="tax-meta-field" /></td>
|
||||
<td><input type="text" name="' . MTEE_NAME_KEYWORDS . '"
|
||||
value="' . $input_values['keywords'] . '"
|
||||
placeholder="' . $input_values['keyword_pl'] . '"
|
||||
class="tax-meta-field" /></td>
|
||||
</tr>
|
||||
<tr class="form-field">
|
||||
<th>meta description</th>
|
||||
<td><input type="text" name="' . MTEE_NAME_DESCRIPTION . '" value="' . $desc_value . '" class="tax-meta-field" /></td>
|
||||
<td><input type="text" name="' . MTEE_NAME_DESCRIPTION . '"
|
||||
value="' . $input_values['desc'] . '"
|
||||
placeholder="' . $input_values['desc_pl'] . '"
|
||||
class="tax-meta-field" /></td>
|
||||
</tr>
|
||||
';
|
||||
}
|
||||
|
||||
function set_input_values($tag): array {
|
||||
$key_value = '';
|
||||
$desc_value = '';
|
||||
$keyword_placeholder = '';
|
||||
$desc_placeholder = '';
|
||||
if (isset($tag->term_id)) {
|
||||
$key_value = get_term_meta($tag->term_id, MTEE_NAME_KEYWORDS, true);
|
||||
$desc_value = get_term_meta($tag->term_id, MTEE_NAME_DESCRIPTION, true);
|
||||
$keyword_placeholder = $this->set_default_keywords($tag);
|
||||
$desc_placeholder = $this->set_default_description($tag);
|
||||
}
|
||||
return array(
|
||||
'keywords' => $key_value,
|
||||
'desc' => $desc_value,
|
||||
'keyword_pl' => $keyword_placeholder,
|
||||
'desc_pl' => $desc_placeholder,
|
||||
);
|
||||
}
|
||||
|
||||
public function set_default_keywords($tag) {
|
||||
if ($tag->taxonomy == 'category') {
|
||||
$keywords = $this->options['keywords_tmp']['category'];
|
||||
} elseif ($tag->taxonomy == 'post_tag') {
|
||||
$keywords = $this->options['keywords_tmp']['tag'];
|
||||
} else {
|
||||
$keywords = $this->options['keywords_tmp']['tax'];
|
||||
}
|
||||
if (empty($keywords)) {
|
||||
$keywords = '##title##,##description##,##site_name##';
|
||||
}
|
||||
return $keywords;
|
||||
}
|
||||
|
||||
public function set_default_description($tag) {
|
||||
if ($tag->taxonomy == 'category') {
|
||||
$description = $this->options['description_tmp']['category'];
|
||||
} elseif ($tag->taxonomy == 'post_tag') {
|
||||
$description = $this->options['description_tmp']['tag'];
|
||||
} else {
|
||||
$description = $this->options['description_tmp']['tax'];
|
||||
}
|
||||
if (empty($description)) {
|
||||
$description = '##site_name##の' . MTEE_META_DESC_BEFORE_BRACKETS . '##title##' . MTEE_META_DESC_AFTER_BRACKETS . MTEE_META_DESC_SINGLE_BASE;
|
||||
}
|
||||
return $description;
|
||||
}
|
||||
|
||||
function save_terms($term_id) {
|
||||
if (array_key_exists(MTEE_NAME_KEYWORDS, $_POST)) {
|
||||
|
||||
@@ -35,7 +35,7 @@ if ($mtee->is_enable('enabled')) {
|
||||
require_once MTEE_CLASS_DIR . 'mtee_meta_output_keydesc.php';
|
||||
new mtee_tax_setting($options);
|
||||
new mtee_post_setting($options);
|
||||
new mtee_meta_output_keydesc;
|
||||
new mtee_meta_output_keydesc();
|
||||
}
|
||||
|
||||
// enabled my canonical. caution : default canonical delete
|
||||
|
||||
Reference in New Issue
Block a user