add_action('init', function () {
if (!is_admin() || !current_user_can('administrator')) return;
if (get_option('school_content_migrated_all')) return; // 重複実行防止
$pages = get_posts([
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => -1,
]);
foreach ($pages as $page) {
$content = $page->post_content;
// **学校概要ページ**(h2タグに続くテキストがpタグで囲われていない場合も対象)
if (strpos($content, '
') !== false && strpos($content, '
') !== false) {
// 学校概要ページの処理
$content = handle_school_overview_page($content);
}
// **写真/動画ページ**(h2タグをh1に変更し、その下のpタグを追加)
elseif (strpos($content, '写真/動画
') !== false) {
$content = handle_photos_videos_page($content);
}
// **コース費用ページ**(h2タグをh1に変更し、その下のpタグを追加)
elseif (strpos($content, 'コース費用
') !== false) {
$content = handle_course_fee_page($content);
}
// 更新するコンテンツを投稿に適用
wp_update_post([
'ID' => $page->ID,
'post_content' => $content,
]);
}
// 処理済みフラグを設定
update_option('school_content_migrated_all', true);
});
// 学校概要ページ用の処理(h2 → h1、pタグを追加)
function handle_school_overview_page($content) {
// 学校名
→ 日本語名
+
英語名
に変換
$content = preg_replace_callback('#
\s*
\s*
\s*
([^<]+)
\s*(.*?)\s*\s*
\s*
#u', function ($matches) {
$jp_name = trim($matches[1]); // 日本語名(h2)
$text_content = trim($matches[2]); // 下のテキスト(英語名)
// テキストがpタグで囲まれていない場合、pタグで囲む
if (strpos($text_content, '
') === false) {
$text_content = "
{$text_content}
";
}
// 入れ替えた構造を返す
return "
{$jp_name}
{$text_content}
";
}, $content);
return $content;
}
// 写真/動画ページ用の処理(h2 → h1、pタグを追加)
function handle_photos_videos_page($content) {
// h2 → h1 に変換(その下のテキストがpタグで囲われていなければ囲む)
$content = preg_replace('#
([^<]+)
#u', '
$1
', $content);
// h1の下のテキストがpタグで囲まれていなければpタグを追加
$content = preg_replace_callback('#
([^<]+)
\s*([^<]+)#u', function ($matches) {
$h1_text = $matches[1]; // h1のテキスト
$p_text = $matches[2]; // h1の下のテキスト
// pタグで囲まれていなければpタグを追加
if (strpos($p_text, '
') === false) {
$p_text = "
{$p_text}
";
}
// 入れ替えた構造を返す
return "
{$h1_text}
{$p_text}";
}, $content);
return $content;
}
// コース費用ページ用の処理(h2 → h1、pタグを追加)
function handle_course_fee_page($content) {
// h2 → h1 に変換(その下のテキストがpタグで囲われていなければ囲む)
$content = preg_replace('#
([^<]+)
#u', '
$1
', $content);
// h1の下のテキストがpタグで囲われていなければpタグを追加
$content = preg_replace_callback('#
([^<]+)
\s*([^<]+)#u', function ($matches) {
$h1_text = $matches[1]; // h1のテキスト
$p_text = $matches[2]; // h1の下のテキスト
// pタグで囲まれていなければpタグを追加
if (strpos($p_text, '
') === false) {
$p_text = "
{$p_text}
";
}
// 入れ替えた構造を返す
return "
{$h1_text}
{$p_text}";
}, $content);
return $content;
}
Warning: Cannot modify header information - headers already sent by (output started at /home/lalalaplus/lalalaaustralia.com/public_html/wp-content/themes/twentyseventeen-child/functions.php:406) in
/home/lalalaplus/lalalaaustralia.com/public_html/wp-includes/pluggable.php on line
1435
Warning: Cannot modify header information - headers already sent by (output started at /home/lalalaplus/lalalaaustralia.com/public_html/wp-content/themes/twentyseventeen-child/functions.php:406) in
/home/lalalaplus/lalalaaustralia.com/public_html/wp-includes/pluggable.php on line
1438