投稿ページとアイキャッチ画像への画像挿入、メディアライブラリへの直アップ時のファイルの自動変換コード。
以下のコードは3つのコードの分かれている。
- 新規にページに貼り付ける画像のタイトルを「記事ID = ◯◯◯」に、それと「ALT」を自動挿入するコード。ALTは記事のタイトルをそのまま入れることになる。
- 画像アップロード時に画像の「ファイル名」を「post-id-◯◯◯◯◯」に自動変換する。
- 右サイドの「アイキャッチ画像を設定」でアップロードされた場合のみ、ファイル名を「記事ID」に変更する
実際にfanction.phpに入れているコードがこれになる。2026年10月2日最終コード
/**
* 1. 画像アップロード時は、一旦すべて「本文画像」のルールで命名&ページのタイトルをALTに設定
*/
add_filter('wp_handle_upload', 'custom_rename_all_uploads_to_content', 10, 2);
function custom_rename_all_uploads_to_content($upload, $context) {
$post_id = 0;
if (isset($_POST['post_id'])) { $post_id = intval($_POST['post_id']); }
elseif (isset($_REQUEST['post'])) { $post_id = intval($_REQUEST['post']); }
elseif (isset($_REQUEST['post_id'])) { $post_id = intval($_REQUEST['post_id']); }
if (!$post_id) { return $upload; }
$file_path = $upload['file'];
$file_dir = dirname($file_path);
$ext = '.' . pathinfo($file_path, PATHINFO_EXTENSION);
$new_filename_base = 'post-id-' . $post_id; //ページ貼り込みの画像のファイル名
$unique_filename = wp_unique_filename($file_dir, $new_filename_base . $ext);
$new_file_path = $file_dir . '/' . $unique_filename;
if (rename($file_path, $new_file_path)) {
$upload['file'] = $new_file_path;
$upload['url'] = str_replace(basename($file_path), $unique_filename, $upload['url']);
}
return $upload;
}
add_filter('wp_insert_attachment_data', 'custom_rename_all_titles_to_content', 10, 2);
function custom_rename_all_titles_to_content($data, $postarr) {
$post_id = 0;
if (isset($_POST['post_id'])) { $post_id = intval($_POST['post_id']); }
elseif (isset($_REQUEST['post'])) { $post_id = intval($_REQUEST['post']); }
elseif (isset($_REQUEST['post_id'])) { $post_id = intval($_REQUEST['post_id']); }
if (!$post_id) { return $data; }
$data['post_title'] = '投稿記事_ID = ' . $post_id; //ページ貼り込みの画像のタイトル
return $data;
}
// 初期アップロード時に「投稿ページのタイトル」を代替テキスト(ALT)に設定
add_action('add_attachment', 'custom_set_initial_alt_text_from_page_title');
function custom_set_initial_alt_text_from_page_title($attachment_id) {
$post_id = 0;
if (isset($_POST['post_id'])) { $post_id = intval($_POST['post_id']); }
elseif (isset($_REQUEST['post'])) { $post_id = intval($_REQUEST['post']); }
elseif (isset($_REQUEST['post_id'])) { $post_id = intval($_REQUEST['post_id']); }
if ($post_id) {
// 投稿ページのタイトルを取得
$page_title = get_the_title($post_id);
// もしページタイトルが空(タイトル未入力)の場合はフォールバック(この設定に代替えするという意味)
if (empty($page_title)) {
$page_title = '本文?画像_' . $post_id;
}
update_post_meta($attachment_id, '_wp_attachment_image_alt', $page_title);
}
}
/**
* 2. アイキャッチ画像として投稿に設定された瞬間に、ファイル名・タイトルを書き換え、ALTにページタイトルを設定
*/
add_action('added_post_meta', 'custom_rename_when_featured_image_set', 10, 4);
add_action('updated_post_meta', 'custom_rename_when_featured_image_set', 10, 4);
function custom_rename_when_featured_image_set($meta_id, $post_id, $meta_key, $meta_value) {
if ($meta_key !== '_thumbnail_id') { return; }
$attachment_id = intval($meta_value);
if (!$attachment_id) { return; }
$current_file = get_attached_file($attachment_id);
if (strpos(basename($current_file), 'thumb-') === 0) { return; }
// ① メディアタイトルの書き換え
wp_update_post(array(
'ID' => $attachment_id,
'post_title' => '投稿アイキャッチ画像_ID = ' . $post_id //アイキャッチ画像のタイトル
));
// ② 代替テキスト(ALT)に「投稿ページのタイトル」を設定
$page_title = get_the_title($post_id);
if (empty($page_title)) {
$page_title = 'アイキャッチ??_' . $post_id;
}
update_post_meta($attachment_id, '_wp_attachment_image_alt', $page_title);
// ③ 実際のファイル名のリネーム処理
if ($current_file && file_exists($current_file)) {
$file_dir = dirname($current_file);
$ext = '.' . pathinfo($current_file, PATHINFO_EXTENSION);
$new_filename_base = 'eye-catch-img-post-id-' . $post_id; //アイキャッチ画像のファイル名
$unique_filename = wp_unique_filename($file_dir, $new_filename_base . $ext);
$new_file_path = $file_dir . '/' . $unique_filename;
if (rename($current_file, $new_file_path)) {
update_attached_file($attachment_id, $new_file_path);
$metadata = wp_get_attachment_metadata($attachment_id);
if ($metadata && isset($metadata['file'])) {
$old_basename = basename($current_file);
$metadata['file'] = str_replace($old_basename, $unique_filename, $metadata['file']);
if (isset($metadata['sizes']) && is_array($metadata['sizes'])) {
foreach ($metadata['sizes'] as $size_key => $size_info) {
$old_size_file = $file_dir . '/' . $size_info['file'];
if (file_exists($old_size_file)) {
$new_size_file_name = str_replace(pathinfo($old_basename, PATHINFO_FILENAME), pathinfo($unique_filename, PATHINFO_FILENAME), $size_info['file']);
rename($old_size_file, $file_dir . '/' . $new_size_file_name);
$metadata['sizes'][$size_key]['file'] = $new_size_file_name;
}
}
}
wp_update_attachment_metadata($attachment_id, $metadata);
}
}
}
}このコード、実際は重なって設定している項目があるようなので、暇な時に修正し直す必要がある。
ファイル名の変換がそれにあたるような気がする。
