/
kdesc.php
101 lines (69 loc) · 2.38 KB
/
kdesc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/*
Plugin Name: KDESC
Plugin URI: https://github.com/b9sk/kdesc
Description: Adding keywords field for tag and category taxonomies. Rewrite title-tag. Output META description and META keywords to HEAD.
Version: 0.4
Author: Your mom
License: Free
*/
/**
* Дополнительные поля keywords, description, title для tag и category
*/
require_once("Tax-meta-class/Tax-meta-class.php");
if (is_admin()) {
$kdesc_config = array(
'id' => 'kdesc_keywords_meta_box',
'title' => 'Add Keywords',
'pages' => array('category', 'post_tag'),
'context' => 'normal',
'fields' => array(),
'use_with_theme' => false,
);
$kdesc_field = new Tax_Meta_Class( $kdesc_config );
$kdesc_field->addText( 'm_kdesc_keywords', array('name'=> __('Meta Keywords', 'tax-meta'), 'desc' => 'Custom keywords for this taxonomy.' ) );
$kdesc_field->addText( 'm_kdesc_description', array('name'=> __('Meta Description', 'tax-meta'), 'desc' => 'Custom description for this taxonomy.' ) );
$kdesc_field->addText( 'm_kdesc_title', array('name'=> __('Custom title', 'tax-meta'), 'desc' => 'Rewrite title-tag.' ) );
$kdesc_field->Finish();
}
/**
* Вывод keywords и description
*/
function kdesc_add_meta( $name, $content ) {
if ( $content )
echo '<meta name="'.$name.'" itemprop="'.$name.'" content="'.$content.'">';
}
add_action( 'wp_head_add_meta', 'kdesc_add_meta', 10, 2 );
add_action( 'wp_head', function() {
if ( is_tag() ) {
$tax_id = get_query_var('tag_id');
}
if ( is_category() ) {
$tax_id = get_query_var('cat');
}
if ( is_category() || is_tag() ) {
$kdesc_keywords_data = get_tax_meta(
$tax_id,
'm_kdesc_keywords'
);
$kdesc_description_data = get_tax_meta(
$tax_id,
'm_kdesc_description'
);
do_action('wp_head_add_meta', 'keywords', $kdesc_keywords_data);
do_action('wp_head_add_meta', 'description', $kdesc_description_data);
}
} ); //---/wp_head---
/*
* Перезаписать title
*/
add_filter( 'wp_title', 'kdesc_title_tag_rewtite' );
function kdesc_title_tag_rewtite($title) {
if ( is_category() || is_tag() ) {
$tax_id = is_category() ? get_query_var('cat') : get_query_var('tag_id');
$title = get_tax_meta($tax_id, 'm_kdesc_title') ?
get_tax_meta($tax_id, 'm_kdesc_title') . ' | ' . get_bloginfo( 'name' ) :
$title;
}
return $title;
}