-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
447 lines (407 loc) · 13.8 KB
/
functions.php
File metadata and controls
447 lines (407 loc) · 13.8 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
/**
* 初期セットアップ
*/
add_action('after_setup_theme', 'noota_setup');
function noota_setup() {
// titleタグ設定
add_theme_support('title-tag');
// フィードのリンクタグ設定
add_theme_support('automatic-feed-links');
// アイキャッチ画像設定
add_theme_support('post-thumbnails');
// 検索フォーム設定
add_theme_support( 'html5', array( 'search-form' ) );
// カスタムロゴ設定
add_theme_support( 'custom-logo' );
// カスタムメニュー有効化
register_nav_menus(array(
'main-menu' => esc_html('Main Menu'),
'footer-menu' => esc_html('Footer Menu')
));
}
/**
* titleタグセパレート文字設定
*/
add_filter('document_title_separator', 'noota_document_title_separator');
function noota_document_title_separator($sep)
{
$sep = '|';
return $sep;
}
/**
* 記事中の続きを読む設定
*/
add_filter( 'the_content_more_link', 'blankslate_read_more_link' );
function blankslate_read_more_link() {
if ( ! is_admin() ) {
return ' ...';
}
}
/**
* 抜粋文の続きを読む設定
*/
add_filter( 'excerpt_more', 'blankslate_excerpt_read_more_link' );
function blankslate_excerpt_read_more_link( $more ) {
if ( ! is_admin() ) {
global $post;
return ' ...';
}
}
/**
* CSS・Javascript読み込み設定
*/
add_action('wp_enqueue_scripts', 'noota_load_scripts');
function noota_load_scripts()
{
wp_enqueue_style('noota-style', get_stylesheet_uri());
wp_enqueue_style('noota-common', get_template_directory_uri().'/css/common.css');
wp_enqueue_script('jquery');
wp_enqueue_script('bundle', get_stylesheet_directory_uri() . '/js/bundle.js', array('jquery'));
}
/**
* 画像生成制御
*/
add_filter('intermediate_image_sizes_advanced', 'noota_image_insert_override');
function noota_image_insert_override($sizes)
{
unset($sizes['medium_large']);
return $sizes;
}
/**
* ウィジェット設定
*/
add_action('widgets_init', 'noota_widgets_init');
function noota_widgets_init()
{
register_sidebar(array(
'name' => esc_html('Sidebar Widget Area'),
'id' => 'primary-widget-area',
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => '</li>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
));
}
/**
* ピンバックタグ設定
*/
add_action( 'wp_head', 'pingback_header' );
function pingback_header() {
if ( is_singular() && pings_open() ) {
printf( '<link rel="pingback" href="%s" />' . "\n", esc_url( get_bloginfo( 'pingback_url' ) ) );
}
}
/**
* コメント返信用js設定
*/
add_action( 'comment_form_before', 'enqueue_comment_reply_script' );
function enqueue_comment_reply_script() {
if ( get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
/**
* トラックバック・ピンバックDOM設定
* comments.php内wp_list_comments()のコールバックとして使用
*/
function custom_pings( $comment ) {
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>"><?php echo comment_author_link(); ?></li>
<?php
}
/**
* コメント数カウント設定
* wp_list_comments()からトラックバック・ピンバックのカウントを除外
*/
add_filter( 'get_comments_number', 'comment_count', 0 );
function comment_count( $count ) {
if ( ! is_admin() ) {
global $id;
$get_comments = get_comments( 'status=approve&post_id=' . $id );
$comments_by_type = separate_comments( $get_comments );
return count( $comments_by_type['comment'] );
} else {
return $count;
}
}
/**
* 管理画面に設定項目追加
* 参考:https://www.nxworld.net/wordpress/wp-add-settings-field.html
*/
function search_field() {
// 検索機能無効化
add_settings_field( 'search_disable', '検索機能', 'search_option', 'discussion' );
register_setting( 'discussion', 'search_disable' );
}
add_filter( 'admin_init', 'search_field' );
/* 管理画面設定項目DOM */
function search_option() {
// 検索機能無効化
?>
<fieldset>
<p class="description">検索フォームの機能を以下のパターンから選択できます。</p>
<label><input name="search_disable" type="radio" value="enable" <?php checked( 'enable', get_option( 'search_disable' ) ); ?> />検索機能を有効にする</label><br>
<label><input name="search_disable" type="radio" value="disable" <?php checked( 'disable', get_option( 'search_disable' ) ); ?> />検索機能を無効にする</label><br>
<label><input name="search_disable" type="radio" value="shifter_algolia" <?php checked( 'shifter_algolia', get_option( 'search_disable' ) ); ?> />ShifterWordPressとAlgoliaを併用する</label>
</fieldset>
<?php
}
/* SNS設定 */
function share_field() {
add_settings_field( 'share_option', 'SNS設定', 'share_options', 'discussion' );
register_setting( 'discussion', 'twitter_via' );
register_setting( 'discussion', 'twitter_related' );
register_setting( 'discussion', 'twitter_hashtags' );
}
add_filter( 'admin_init', 'share_field' );
function share_options() {
// Twitter設定
?>
<fieldset>
<p class="description">【Twitter】</p>
<p class="description">ツイート内に含むユーザ名</p>
@<input name="twitter_via" id="twitter_via" type="text" value="<?php echo esc_html( get_option( 'twitter_via' ) ); ?>" maxlength="15" class="regular-text" />
<p class="description">ツイート後に表示されるユーザー</p>
@<input name="twitter_related" id="twitter_related" type="text" value="<?php echo esc_html( get_option( 'twitter_related' ) ); ?>" maxlength="15" class="regular-text" />
<p class="description">ハッシュタグ</p>
#<input name="twitter_hashtags" id="twitter_hashtags" type="text" value="<?php echo esc_html( get_option( 'twitter_hashtags' ) ); ?>" maxlength="100" class="regular-text" />
</fieldset>
<fieldset>
<p class="description">【Facebook】</p>
<p class="description">Facebook APP ID</p>
<input name="facebook_app_id " id="facebook_app_id " type="text" value="<?php echo esc_html( get_option( 'facebook_app_id ' ) ); ?>" maxlength="20" class="regular-text" />
</fieldset>
<?php
}
/**
* OGP設定
* 参考:https://www.torat.jp/wordpress-setting-ogp/
*/
function my_meta_ogp() {
if (is_front_page() || is_home() || is_singular()) {
/*初期設定*/
// 画像 (アイキャッチ画像が無い時に使用する代替画像URL)
$ogp_image = get_template_directory_uri() . "/img/no_image-lg.png";
// Twitterのアカウント名 (@xxx)
$twitter_site = '@'.esc_html( get_option( 'twitter_via' ) );
// Twitterカードの種類(summary_large_image または summary を指定)
$twitter_card = 'summary_large_image';
// Facebook APP ID
$facebook_app_id = '';
/*初期設定 ここまで*/
global $post;
$ogp_title = '';
$ogp_description = '';
$ogp_url = '';
$html = '';
if (is_singular()) {
// 記事&固定ページ
setup_postdata($post);
$ogp_title = $post->post_title;
$ogp_description = mb_substr(get_the_excerpt(), 0, 100);
$ogp_url = get_permalink();
wp_reset_postdata();
} elseif (is_front_page() || is_home()) {
// トップページ
$ogp_title = get_bloginfo('name');
$ogp_description = get_bloginfo('description');
$ogp_url = home_url();
}
// og:type
$ogp_type = (is_front_page() || is_home()) ? 'website' : 'article';
// og:image
if (is_singular() && has_post_thumbnail()) {
$ps_thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
$ogp_image = $ps_thumb[0];
}
// 出力するOGPタグをまとめる
$html = "\n";
$html .= '<meta property="og:title" content="' . esc_attr($ogp_title) . '">' . "\n";
$html .= '<meta property="og:description" content="' . esc_attr($ogp_description) . '">' . "\n";
$html .= '<meta property="og:type" content="' . $ogp_type . '">' . "\n";
$html .= '<meta property="og:url" content="' . esc_url($ogp_url) . '">' . "\n";
$html .= '<meta property="og:image" content="' . esc_url($ogp_image) . '">' . "\n";
$html .= '<meta property="og:site_name" content="' . esc_attr(get_bloginfo('name')) . '">' . "\n";
$html .= '<meta name="twitter:card" content="' . $twitter_card . '">' . "\n";
$html .= '<meta name="twitter:site" content="' . $twitter_site . '">' . "\n";
$html .= '<meta property="og:locale" content="ja_JP">' . "\n";
if ($facebook_app_id != "") {
$html .= '<meta property="fb:app_id" content="' . $facebook_app_id . '">' . "\n";
}
echo $html;
}
}
// headタグ内にOGPを出力する
add_action('wp_head', 'my_meta_ogp');
/**
* 検索機能を無効化
* 参考:https://www.lancork.net/2015/04/how-to-disable-wordpress-search-function/
*/
function fb_filter_query( $query, $error = true ) {
if ( is_search() && get_option( 'search_disable' ) === 'disable' ) {
$query->is_search = false;
$query->query_vars['s'] = false;
$query->query['s'] = false;
// to error
if ( $error == true ) {
$query->is_404 = true;
}
}
}
add_action( 'parse_query', 'fb_filter_query' );
add_filter( 'get_search_form', function($a){return null;} );
/**
* 記事内最初の画像を取得
* $get_size: 取得する画像のサイズ
* $altimg_id: 代替画像のID。(画像はあらかじめメディアライブラリからアップロードしておく)
* nullの場合、投稿内に画像が無ければ何も出力しない
* 参考:https://qiita.com/ryujisanagi/items/96d5d67bb9fc4cf8315a
*/
function catch_thumbnail_image($get_size = 'full', $altimg_id = null) {
global $post;
$image = '';
$image_id = '';
$image_get = preg_match_all( '/<img.+class=[\'"].*wp-image-([0-9]+).*[\'"].*>/i', $post->post_content, $matches );
if( isset($matches[1][0]) ) {
$image_id = $matches[1][0];
}
if( !$image_id && $altimg_id ){
$image_id = $altimg_id;
}
$image = wp_get_attachment_image( $image_id, $get_size, false, array(
'class' => 'thumbnail-image',
'srcset' => wp_get_attachment_image_srcset( $image_id, $get_size ),
'sizes' => wp_get_attachment_image_sizes( $image_id, $get_size )
) );
if( empty($image) ) {
$image = false;
}
return $image;
}
/**
* bloginfo()をショートコード化
* Plugin Name: Bloginfo Shortcode
* Description: Allows bloginfo() as a shortcode.
* Author: Giuseppe Mazzapica
* Author URI: http://gm.zoomlab.it
* License: MIT
* 使い方:[bloginfo info='name']
*/
add_shortcode('bloginfo', function($atts) {
$atts = shortcode_atts(array('filter'=>'', 'info'=>''), $atts, 'bloginfo');
$infos = array(
'name', 'description',
'wpurl', 'url', 'pingback_url',
'admin_email', 'charset', 'version', 'html_type', 'language',
'atom_url', 'rdf_url','rss_url', 'rss2_url',
'comments_atom_url', 'comments_rss2_url',
);
$filter = in_array(strtolower($atts['filter']), array('raw', 'display'), true)
? strtolower($atts['filter'])
: 'display';
return in_array($atts['info'], $infos, true) ? get_bloginfo($atts['info'], $filter) : '';
});
/**
* 目次を追加
* add_filter('the_content', 'add_index');
* 上記を追加することでthe_content()内に自動追加
* 参考:https://u-web-nana.com/function-table-of-contents/
*/
/* 目次に合わせて見出しにID付与 */
function generate_index($content) {
// 目次用コンテンツを生成
if (is_single()) {
$elements = search_index($content);
// 1つ以上該当の要素があったら
if (count($elements) >= 1) {
// 初期化
$toc = '';
$i = 0;
$currentlevel = 0;
$id = 'chapter-';
foreach ($elements as $element) {
// 順番に応じてid属性を付与
$id .= $i + 1;
$replace_title = preg_replace('/<(h[1-6])>(.+?)<\/(h[1-6])>/s', '<$1 id="' . $id . '" class="js-index-heading">$2</$3>', $element[0]);
$content = str_replace($element[0], $replace_title, $content);
$i++;
$id = 'chapter-';
} // end foreach
$h2 = '/<h2.*?>/i';
if (preg_match($h2, $content, $h2s)) {
$content = preg_replace($h2, $h2s[0], $content, 1);
}
}
}
return $content;
}
/* 見出しの数検索 */
function search_index($content = ''){
$elements = false;
if (is_single()) {
if(!$content){$content = get_the_content();}
// 正規表現でid属性を持たないh1~h6を検索
$pattern = '/<h[1-6](?!.*id=").*?>(.*?)<\/h[1-6]>/i';
preg_match_all($pattern, $content, $elements, PREG_SET_ORDER);
}
return $elements;
}
add_filter('the_content', 'generate_index');
/* 目次DOM生成 */
function add_index($content = ''){
if (is_single()) {
if(!$content){$content = get_the_content();}
// id付与済みのcontentを取得
$elements = search_index($content);
// 1つ以上該当の要素があったら
if (count($elements) >= 1) {
// 初期化
$toc = '';
$i = 0;
$currentlevel = 0;
$id = 'chapter-';
foreach ($elements as $element) {
$id .= $i + 1;
// ネストを計算
if (strpos($element[0], '<h2') !== false) {
$level = 1;
} elseif (strpos($element[0], '<h3') !== false) {
$level = 2;
} elseif (strpos($element[0], '<h4') !== false) {
$level = 3;
} elseif (strpos($element[0], '<h5') !== false) {
$level = 4;
} elseif (strpos($element[0], '<h6') !== false) {
$level = 5;
}
// ネスト用に要素を追加
while ($currentlevel < $level) {
if ($currentlevel === 0) {
$toc .= '<ul class="index-list">';
} else {
$toc .= '<ul class="index-list_child">';
}
$currentlevel++;
}
while ($currentlevel > $level) {
$toc .= '</li></ul>';
$currentlevel--;
}
// 目次の項目で使用する要素を指定
$toc .= '<li class="index-item"><a href="#' . $id . '" class="index-link js-index">' . $element[1] . '</a>';
$i++;
$id = 'chapter-';
} // end foreach
// 目次の最後の項目をどの要素から作成したかによりタグの閉じ方を変更
while ($currentlevel > 0) {
$toc .= '</li></ul>';
$currentlevel--;
}
// 目次出力用に最終整形
$index = '<div class="single-index index">' . $toc . '</div>';
}
}
return $index;
}
add_shortcode( 'add_index', 'add_index' );