-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject-specific.php
More file actions
626 lines (540 loc) · 24.6 KB
/
Copy pathproject-specific.php
File metadata and controls
626 lines (540 loc) · 24.6 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
<?php
// [NOTICE!]
// こちらの関数は、直接このファイルを読み込まず、function.phpに直接コピペして個別のプロジェクトごとに調整してください。
// ==========================================================
// セキュリティ系
// 特別な理由がなければ適用するべき項目です。
// ----------------------------------------------------------
// 未ログイン時のREST APIアクセスを制限
add_filter('rest_authentication_errors', function ($result) {
if (!is_user_logged_in()) {
return new WP_Error('rest_forbidden', 'REST API is disabled for unauthenticated users.', ['status' => 401]);
}
return $result;
});
// ==========================================================
// カスタマイズ系
function is_karappo_admin() {
$current_user = wp_get_current_user();
return $current_user->user_login == 'KarappoAdmin';
}
// ----------------------------------------------------------
// KarappAdmin以外のユーザーの時、サイドバーのメニューから不要なものを非表示
// 下記サイトで調べるとメニューの消し方がすぐ分かる
// https://plugintests.com/plugins/{author}/{plugin}/tips
// ※ プラグイン名の後に"/tips"を付与
// 例(ACF): https://plugintests.com/plugins/wporg/advanced-custom-fields/tips
function custom_admin_menu() {
if(!is_karappo_admin()) {
remove_submenu_page('index.php', 'update-core.php'); //更新
remove_menu_page('edit-comments.php'); // コメント
remove_menu_page('themes.php'); // 外観
remove_menu_page('plugins.php'); // プラグイン
remove_menu_page('tools.php'); // ツール
remove_menu_page('options-general.php'); // 設定
remove_menu_page('edit.php' ); // 投稿を非表示
remove_menu_page('edit.php?post_type=page'); // 固定ページ
//remove_menu_page('upload.php'); // メディア
//remove_submenu_page( 'upload.php', 'media-new.php' ); //新規追加
// プラグイン
remove_menu_page('edit.php?post_type=acf-field-group'); // ACF
remove_menu_page('wpcf7'); // Contact Form 7
remove_menu_page('simple_history_admin_menu_page'); // Simple History
}
// サイドバーに特定の固定ページ(例としてid:8)の編集画面へのリンクを追加
add_menu_page(
'特定の固定ページ編集',
'○○ページ',
'edit_pages', // 固定ページの編集権限を持つユーザーにのみ表示
'post.php?post=8&action=edit', // 編集画面へのリンク
'',
'dashicons-edit',
20
);
echo '';
}
add_action( 'admin_menu', 'custom_admin_menu' );
// ----------------------------------------------------------
// 「更新してください」を非表示に
function hide_update_notifications_for_non_admins() {
if(!is_karappo_admin()) {
// アップデート通知を削除
remove_action('admin_notices', 'update_nag', 3); // 「WordPress x.x.x が利用可能です ! 今すぐ更新してください。」を消す
remove_action('admin_notices', 'maintenance_nag'); // メンテナンス通知
add_filter('pre_site_transient_update_core', '__return_null');
add_filter('pre_site_transient_update_plugins', '__return_null');
add_filter('pre_site_transient_update_themes', '__return_null');
// プラグインページの「更新通知バッジ」などを非表示にする
global $wp_filter;
if (isset($wp_filter['admin_footer'])) {
unset($wp_filter['admin_footer']);
}
}
}
add_action('admin_init', 'hide_update_notifications_for_non_admins', 1);
// ----------------------------------------------------------
// ダッシュボードの「PHP の更新を推奨」ウィジェット(WordPress 5.6以降)などを非表示にする
function hide_dashboard_boxes_for_clients() {
if(!is_karappo_admin()) {
echo '
<style>
#dashboard_php_nag,
/*#dashboard_site_health,*/
#dashboard_right_now,
#dashboard_primary,
#dashboard_secondary,
#dashboard_quick_press,
#dashboard_activity {
display: none !important;
}
</style>
';
}
}
add_action('admin_head', 'hide_dashboard_boxes_for_clients');
// ----------------------------------------------------------
// KarappAdmin以外のユーザーの時、管理バーの上部に表示される項目を非表示
function my_remove_adminbar_menu($wp_admin_bar) {
if(!is_karappo_admin()) {
$wp_admin_bar->remove_menu('wp-logo'); // WPロゴ
// $wp_admin_bar->remove_menu('site-name'); // サイト名
$wp_admin_bar->remove_menu('view-site'); // サイト名 -> サイトを表示
$wp_admin_bar->remove_menu('plugins'); // サイト名 -> プラグイン
$wp_admin_bar->remove_menu('dashboard'); // サイト名 -> ダッシュボード (公開側)
$wp_admin_bar->remove_menu('themes'); // サイト名 -> テーマ (公開側)
$wp_admin_bar->remove_menu('customize'); // サイト名 -> カスタマイズ (公開側)
$wp_admin_bar->remove_menu('comments'); // コメント
$wp_admin_bar->remove_menu('updates'); // 更新
$wp_admin_bar->remove_menu('view'); // 投稿を表示
$wp_admin_bar->remove_menu('new-content'); // 新規
$wp_admin_bar->remove_menu('new-post'); // 新規 -> 投稿
$wp_admin_bar->remove_menu('new-media'); // 新規 -> メディア
$wp_admin_bar->remove_menu('new-link'); // 新規 -> リンク
$wp_admin_bar->remove_menu('new-page'); // 新規 -> 固定ページ
$wp_admin_bar->remove_menu('new-user'); // 新規 -> ユーザー
// $wp_admin_bar->remove_menu('my-account'); // マイアカウント
// $wp_admin_bar->remove_menu('user-info'); // マイアカウント -> プロフィール
// $wp_admin_bar->remove_menu('edit-profile'); // マイアカウント -> プロフィール編集
// $wp_admin_bar->remove_menu('logout'); // マイアカウント -> ログアウト
$wp_admin_bar->remove_menu('search'); // 検索 (公開側)
}
}
add_action('admin_bar_menu', 'my_remove_adminbar_menu', 201);
// ----------------------------------------------------------
// 固定ページの一覧ページにきたとしたら、特定の固定ページ(例としてid:8)へリダイレクト
function redirect_pages_list_for_specific_users() {
if (!is_karappo_admin() && is_admin()) {
global $pagenow;
if ($pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'page') {
wp_redirect(admin_url('post.php?post=8&action=edit'));
exit;
}
}
}
add_action('admin_init', 'redirect_pages_list_for_specific_users');
// ----------------------------------------------------------
// Gutenbergエディタから使用するブロックだけを表示
// ブロックタイプはこちらを参照
// https://wpqw.jp/wordpress/block-editor/remove-blocks58/
function my_plugin_allowed_block_types_all( $allowed_block_types, $block_editor_context ) {
// 許可するブロックタイプ
$allowed_block_types = [
'core/audio',
'core/paragraph',
'core/heading',
'core/image',
'core/list',
'core/list-item', // リストアイテム(リストの内部で使用。これがないとリストないで改行しても次のアイテムにならない不具合が発生する)
'core/gallery',
'core/embed',
'core/quote',
'core/separator',
// 'acf/custom-block',
];
return $allowed_block_types;
}
add_filter( 'allowed_block_types_all', 'my_plugin_allowed_block_types_all', 10, 2 );
// embedの中身をjs側で削除
// 詳細は、remove-block.jsを参照
add_action( 'enqueue_block_editor_assets', function() {
wp_enqueue_script( 'remove-block', get_template_directory_uri().'/karappo-common/remove-block.js', [], false, true );
} );
// ----------------------------------------------------------
// Gutenbergエディタから特定の見出しレベルを削除
// https://ja.wordpress.org/team/handbook/block-editor/how-to-guides/curating-the-editor-experience/disable-editor-functionality/
function modify_heading_levels_globally($args, $block_type) {
if ('core/heading' !== $block_type) {
return $args;
}
// H1, H5, H6 を削除
$args['attributes']['levelOptions']['default'] = [2, 3, 4];
return $args;
}
add_filter('register_block_type_args', 'modify_heading_levels_globally', 10, 2);
// ----------------------------------------------------------
// デバッグ用: 管理画面に現在登録されているブロックタイプを出力
// add_action('admin_init', function() {
// $block_registry = WP_Block_Type_Registry::get_instance();
// $all_blocks = $block_registry->get_all_registered();
// echo '<pre>';
// foreach ($all_blocks as $block_name => $block_type) {
// echo esc_html($block_name) . "\n";
// }
// echo '</pre>';
// });
// ----------------------------------------------------------
// 「投稿」をリネーム
function change_admin_menu() {
global $menu;
global $submenu;
// 「投稿」をリネーム
$menu[5][0] = 'HELLO LETTER';
$submenu['edit.php'][5][0] = 'HELLO LETTER'; // "Post"
$submenu['edit.php'][10][0] = 'HELLO LETTERを追加'; // "Add Post"
// $submenu['edit.php'][15][0] = 'Status'; // "Categories"
$submenu['edit.php'][16][0] = 'ジャンル'; // "Tags"
echo '';
}
add_action( 'admin_menu', 'change_admin_menu' );
function change_post_object_label() {
global $wp_post_types;
$labels = &$wp_post_types['post']->labels;
$labels->name = 'HELLO LETTER';
$labels->singular_name = 'HELLO LETTER';
$labels->add_new = '新規HELLO LETTERを追加';
$labels->edit_item = 'HELLO LETTERを編集';
$labels->new_item = 'HELLO LETTER';
$labels->view_item = 'HELLO LETTERを編集';
$labels->search_items = 'HELLO LETTERを探す';
$labels->not_found = 'HELLO LETTERはありません';
$labels->not_found_in_trash = 'ゴミ箱にHELLO LETTERはありません';
}
add_action( 'init', 'change_post_object_label' );
// ----------------------------------------------------------
// アイキャッチ画像を有効にする。
add_theme_support('post-thumbnails');
// ----------------------------------------------------------
// カスタムポストタイプで「revisions」を有効化
function my_custom_revision() {
add_post_type_support('__CPT_NAME__', 'revisions');
}
add_action('init', 'my_custom_revision');
// ----------------------------------------------------------
// 管理画面からメタボックスを削除
function remove_metabox() {
if(!is_karappo_admin()) {
// リビジョン
remove_meta_box('revisionsdiv', '__CPT_NAME__', 'normal');
// ページ属性
remove_meta_box('pageparentdiv', '__CPT_NAME__', 'side');
// スラッグ
// 【注意】 これをしてしまうと、スラッグの変更ができなくなるのでNG!やらないこと!
// remove_meta_box('slugdiv', '__CPT_NAME__', 'normal');
}
}
add_action( 'add_meta_boxes', 'remove_metabox', 20 );
// ----------------------------------------------------------
// カスタムポストタイプ
// src: https://gist.github.com/naokazuterada/5556068
// ------------------------
// register_cpt ヘルパー
//
// CPT と(オプションで)同名カテゴリータクソノミーをまとめて登録する短縮形。
// プロジェクトの functions.php に貼り付けて使う。
//
// 引数:
// $cpt_name CPT slug(例: 'news')。タクソノミーは "{slug}-category" になる
// $name 表示名(例: 'News')。labels に展開される
// $supports supports 配列(例: ['title', 'thumbnail', 'editor'])。'revisions' は自動付与
// $register_taxonomy true の場合、同名カテゴリータクソノミーも登録
// $taxonomy_label タクソノミーの label(省略時は 'カテゴリー')
// $default_term カテゴリー未選択時のデフォルト term(WP 5.5+)。
// ['name' => 表示名, 'slug' => スラッグ] の形で渡す。
// term が存在しなければ自動作成。slug を必ず指定すること
// (省略すると既存 term の照合に失敗して option が更新されない)
// ------------------------
function register_cpt($cpt_name, $name, $supports, $register_taxonomy = false, $taxonomy_label = null, $default_term = null) {
$supports[] = 'revisions';
register_post_type($cpt_name, [
'labels' => [
'name' => $name,
'singular_name' => $name,
'add_new' => $name.'を追加',
'add_new_item' => '新しい'.$name.'を追加',
'edit_item' => $name.'を編集',
'new_item' => '新しい'.$name,
'view_item' => $name.'を表示',
'search_items' => $name.'を探す',
'not_found' => $name.'はありません',
'not_found_in_trash' => 'ゴミ箱に'.$name.'はありません',
'parent_item_colon' => ''
],
'public' => true,
'capability_type' => 'post',
'hierarchical' => false,
'has_archive' => true,
'show_in_rest' => true,
'supports' => $supports
]);
if ($register_taxonomy) {
$taxonomy_args = [
'label' => $taxonomy_label ?? 'カテゴリー',
'show_ui' => true,
'public' => true,
'hierarchical' => true,
'rewrite' => [
'slug' => $cpt_name,
'with_front' => false
],
'show_in_rest' => true,
];
if ($default_term) {
$taxonomy_args['default_term'] = $default_term;
}
register_taxonomy($cpt_name.'-category', $cpt_name, $taxonomy_args);
}
}
// 使用例:
add_action('init', function() {
// CPT のみ
register_cpt('classes', 'Classes', ['title', 'thumbnail', 'editor']);
// CPT + 同名カテゴリータクソノミー
register_cpt('activity', 'Activity', ['title', 'thumbnail', 'editor'], true);
// CPT + カテゴリー + デフォルト term(未選択時に自動付与)
register_cpt('news', 'News', ['title', 'thumbnail', 'editor'], true, null, [
'name' => 'Information',
'slug' => 'information',
]);
});
// ----------------------------------------------------------
// 管理画面の特定のページに説明文を表示
function displayCategoryNotice() {
$screen = get_current_screen();
if ($screen->id === 'edit-category') {
echo '<div class="notice notice-info" style="padding: 10px; margin-top: 15px;">
<p>
<strong>サイト上での表示</strong><br>
<ul style="list-style: disc; margin-left: 2em;"">
<li><a href="/" target="_blank">トップページ</a>の「HELLO LETTER」セクションと<a href="/hello-letter/" target="_blank">HELLO LETTERページ</a>の「PICK UP CATEGORY」セクションには、このページの一覧の上位4つまでが表示されます。</li>
<li><a href="/hello-letter/categories/" target="_blank">HELLO LETTERのカテゴリー一覧ページ</a>での表示順は作成日順で、このページの並び順とは無関係です。</li>
</ul>
<strong>並び替えの方法</strong><br>
<ul style="list-style: disc; margin-left: 2em;">
<li>各行をドラッグ&ドロップで並び替え可能です。</li>
</ul>
</p>
</div>';
} else if ($screen->id === 'edit-post') {
echo '<div class="notice notice-info" style="padding: 10px; margin-top: 15px;">
<p>
HELLO LETTERのトップページで先頭に大きく表示したい記事は、<a href="/wp/wp-admin/post.php?post=186&action=edit">こちら</a>の「PICK UP 投稿」で設定してください。
</p>
</div>';
}
}
add_action('admin_notices', 'displayCategoryNotice');
// ----------------------------------------------------------
// CPT:xxxのページタイトル下かつパーマリンクの上に注釈を追加
function add_works_title_note_script() {
$screen = get_current_screen();
if ($screen && $screen->post_type === 'xxx') {
?>
<script>
jQuery(document).ready(function ($) {
const $slugBox = $('#edit-slug-box');
if ($slugBox.length) {
$('<div>', {
text: 'タイトルはなるべく英数字にしてください。',
css: {
fontSize: '13px',
color: '#666',
marginTop: '6px',
marginBottom: '6px'
}
}).insertBefore($slugBox);
}
});
</script>
<?php
}
}
add_action('admin_footer-post.php', 'add_works_title_note_script');
add_action('admin_footer-post-new.php', 'add_works_title_note_script');
// ----------------------------------------------------------
// CPT:xxx のパーマリンクに注釈を追加
// (新規作成時には、#edit-slug-boxの中身が空なので、その時は表示せず、パーマリンクが表示された時(表示されている時)にのみ注意書きを表示する)
function add_permalink_note_on_slug_box_update() {
$screen = get_current_screen();
if ($screen && $screen->post_type === 'xxx') {
?>
<script>
document.addEventListener('DOMContentLoaded', function () {
const slugBox = document.getElementById('edit-slug-box');
if (!slugBox) return;
function insertNoteIfNeeded() {
const text = slugBox.innerText || slugBox.textContent;
if (text.includes('パーマリンク') && !document.getElementById('custom-slug-note')) {
const note = document.createElement('div');
note.innerHTML =
'↑デフォルトでタイトルそのままになってしまいますが、日本語が含まれている状態では詳細ページが正しく表示されません。<br>' +
'必ず「編集」を押して、<strong>半角の英数字、ハイフン</strong>に変更をお願いします。';
note.id = 'custom-slug-note';
note.style.fontSize = '13px';
note.style.color = '#666';
note.style.marginBottom = '8px';
slugBox.parentNode.insertBefore(note, slugBox.nextSibling);
}
}
insertNoteIfNeeded();
const observer = new MutationObserver(insertNoteIfNeeded);
observer.observe(slugBox, { childList: true, subtree: true });
});
</script>
<?php
}
}
add_action('admin_footer-post.php', 'add_permalink_note_on_slug_box_update');
add_action('admin_footer-post-new.php', 'add_permalink_note_on_slug_box_update');
// ----------------------------------------------------------
// 記事編集画面の右サイドバーの「カテゴリーの追加」を非表示に(旧エディタ用)
add_action('admin_head', function () {
$style = '<style>';
// 非表示に
$taxonomies = [
'visit-condition',
'visit-duration',
'visit-tag',
'visit-category',
'places-category',
'magazine-category'
];
$ids = [];
foreach ($taxonomies as $taxonomy) {
$ids[] = '#'.$taxonomy.'-tabs';
$ids[] = '#'.$taxonomy.'-adder';
}
$style .= implode(',', $ids) . ' { display: none; }';
// 線を消す
$ids = [];
foreach ($taxonomies as $taxonomy) {
$ids[] = '#'.$taxonomy.'-all';
}
$style .= implode(',', $ids) . ' { border: none; }';
$style .= '</style>';
echo $style;
});
// ----------------------------------------------------------
// 記事編集画面の右サイドバーの「カテゴリーの追加」を非表示に(Gutenberg用)
add_action('admin_head', function () {
echo '<style>
button.editor-post-taxonomies__hierarchical-terms-add {
display: none !important;
}
</style>';
});
// ----------------------------------------------------------
// wp_headでhead>titleを出力しない(head.blade.phpで一元管理する)
remove_action( 'wp_head', '_wp_render_title_tag', 1 );
// ----------------------------------------------------------
// デフォルトでは2560px以上の画像は画像名に"-scaled"が付与されて縮小保存される機能に関して…
// 1. 閾値を変更したい場合
function change_big_image_size_threshold( $threshold ) {
return 2048;
}
add_filter('big_image_size_threshold', 'change_big_image_size_threshold', 999, 1);
// 2. 機能自体を無効化したい場合
// add_filter( 'big_image_size_threshold', '__return_false' );
// ----------------------------------------------------------
// 画像のファイル容量の上限(寸法の上限とは別に設ける)
// big_image_size_threshold は「寸法」しか見ないため、寸法が閾値以下でも
// 高品質で重い JPEG はそのまま残り、SNS の OGP 画像でカードが出ない等の原因になる。
// 一定容量を超える JPEG をアップロード時に再圧縮して軽量化する(寸法は維持)。
add_filter('wp_handle_upload', function ($upload) {
$max_filesize = 1.5 * 1024 * 1024; // この容量を超える JPEG が対象
$quality = 82;
if (($upload['type'] ?? '') !== 'image/jpeg') return $upload;
if (filesize($upload['file']) <= $max_filesize) return $upload;
$editor = wp_get_image_editor($upload['file']);
if (is_wp_error($editor)) return $upload;
$editor->set_quality($quality);
$editor->save($upload['file']); // 同じパスへ上書き(寸法は変えない)
return $upload;
});
// ----------------------------------------------------------
// サムネイルサイズを削除
function remove_thumbnail_sizes( $new_sizes ) {
// この二つは管理画面でも使うのでそのまま残すのが無難
// unset( $new_sizes['thumbnail'] ); // 150x150 ピクセル(切り取ってサイズにフィット)
// unset( $new_sizes['medium'] ); // 300x300 ピクセル(比率を維持したまま指定サイズにおさめる)
// これ以降は要判断
unset( $new_sizes['medium_large'] ); // 768x0 ピクセル(比率を維持したまま指定サイズにおさめる)
unset( $new_sizes['large'] ); // 1024x1024 ピクセル(比率を維持したまま指定サイズにおさめる)
unset( $new_sizes['1536x1536'] ); // 1536x1536 ピクセル(比率を維持したまま指定サイズにおさめる)
unset( $new_sizes['2048x2048'] ); // 2048x2048 ピクセル(比率を維持したまま指定サイズにおさめる)
return $new_sizes;
}
add_filter('intermediate_image_sizes_advanced', 'remove_thumbnail_sizes');
// ----------------------------------------------------------
// サムネイルサイズを追加
add_image_size('picture', 163, 163, true);
add_image_size('picture-2x', 326, 326, true);
// ----------------------------------------------------------
// 大きすぎる画像は容量削減のために自動削除
// (big_image_size_thresholdでscaleされていない画像は、リサイズ後に削除)
function txt_domain_delete_fullsize_image($metadata) {
// for debug
// ob_start();
// var_dump( $metadata );
// $test = ob_get_contents();
// ob_end_clean();
// error_log( $test );
$upload_dir = wp_upload_dir();
$full_image_path = trailingslashit($upload_dir['basedir']) . $metadata['file'];
$original_image_path = preg_replace('/\-scaled\./', '.', $full_image_path);
if ($full_image_path !== $original_image_path) {
// 縮小されていたらもとの画像はより大きいサイズなので削除
unlink($original_image_path);
unset( $metadata['original_image'] );
}
return $metadata;
}
add_filter('wp_generate_attachment_metadata', 'txt_domain_delete_fullsize_image');
// ----------------------------------------------------------
// タイトルの改行: [br] を <br>に変換
add_filter('the_title', function($title, $post_id = null) {
// 投稿IDが指定されていない場合は現在の投稿IDを使用
if ($post_id === null) {
$post_id = get_the_ID();
}
// 投稿タイプがprojectsの場合のみ変換
if (get_post_type($post_id) === 'projects') {
return str_replace('[br]', '<br>', $title);
}
return $title;
}, 10, 2);
// 注意書きを追加
function add_works_title_note_script() {
$screen = get_current_screen();
if ($screen && $screen->post_type === 'xxx') {
?>
<script>
jQuery(document).ready(function ($) {
const $slugBox = $('#edit-slug-box');
if ($slugBox.length) {
$('<div>', {
html: '改行したい箇所には<code>[br]</code>を入力してください。',
css: {
fontSize: '13px',
color: '#666',
marginTop: '6px',
marginBottom: '6px'
}
}).insertBefore($slugBox);
}
});
</script>
<?php
}
}
add_action('admin_footer-post.php', 'add_works_title_note_script');
add_action('admin_footer-post-new.php', 'add_works_title_note_script');