-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
144 lines (108 loc) · 3.85 KB
/
functions.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* tanshio functions and definitions
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
* @package tanshio
*/
function create_TOC( $data, $item, $request ) {
$dom = new DOMDocument("1.0", "UTF-8");
@$dom->loadHTML(mb_convert_encoding($item->post_content, 'HTML-ENTITIES', 'utf-8'));
$xml = simplexml_import_dom($dom);
// $json = json_encode($xml);
// $dom_data = json_decode($json,true);
$dom_data = $dom->getElementsByTagName('h2');
foreach ($dom_data as $d) {
$table_of_contents[] = trim($d->nodeValue);
}
$data->data['toc'] = $table_of_contents;
return $data;
}
add_filter( 'rest_prepare_post', 'create_TOC', 10, 4 );
function delete_API_data( $data, $item, $request ) {
$list = ['guid','status','type','expert','author','comment_status','ping_status','sticky','format','meta','_links','excerpt','categories','tags','template','featured_media'];
foreach ($list as $item) {
unset($data->data[$item]);
}
// unset($data->data["_links"]);
return $data;
}
add_filter( 'rest_prepare_post', 'delete_API_data', 10, 3 );
function get_posts_from_cat( $request ) {
$cat = get_category_by_slug($request['slug']);
if (!$cat) {
return null;
}
$request['categories'] = $cat;
$response = new WP_REST_Posts_Controller('post');
$posts = $response->get_items($request);
if ( empty( $posts ) ) {
return null;
}
return $posts;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'slug/v1', '/cat/(?P<slug>.*)', array(
'methods' => 'GET',
'callback' => 'get_posts_from_cat'
) );
} );
function get_posts_from_tag( $request ) {
$tags = get_tags(array('slug' => $request['slug']));
if (!$tags) {
return null;
}
$request['tags'] = $tags[0]->term_id;
$response = new WP_REST_Posts_Controller('post');
$posts = $response->get_items($request);
if ( empty( $posts ) ) {
return null;
}
return $posts;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'slug/v1', '/tag/(?P<slug>.*)', array(
'methods' => 'GET',
'callback' => 'get_posts_from_tag'
) );
} );
// delete
remove_action('wp_head', 'print_emoji_detection_script', 7 );
remove_action('wp_print_styles', 'print_emoji_styles', 10 );
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_print_styles',8);
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'wp_shortlink_wp_head');
function delete_jquery() {
// WordPress本体のjquery.jsを読み込まない
wp_deregister_script('jquery');
}
add_action( 'wp_enqueue_scripts', 'delete_jquery' );
function remove_dns_prefetch( $hints, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
return array_diff( wp_dependencies_unique_hosts(), $hints );
}
return $hints;
}
add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 );
function custom_oembed($code){
if(strpos($code, 'speakerdeck') !== false || strpos($code, 'speakerdeck') !== false){
$html = preg_replace('/ width="\d+"/', '', $code);
$html = preg_replace('/ height="\d+"/', '', $html);
$html = '<div class="m-embed"><div class="m-embed__main m-embed__main--speakerdeck">' . $code . '</div></div>';
return $html;
} elseif (strpos($code, 'slideshare') !== false) {
$html = preg_replace('/ width="\d+"/', '', $code);
$html = preg_replace('/ height="\d+"/', '', $html);
$html = preg_replace('/<ifr/', '<div class="m-embed"><div class="m-embed__main m-embed__main--slideshare"><ifr', $html);
$html = preg_replace('/<\/iframe>/', '</iframe></div></div>', $html);
return $html;
}
return $code;
}
add_filter('embed_handler_html', 'custom_oembed');
add_filter('embed_oembed_html', 'custom_oembed');