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 |
/** * WordPressで画像をアップロードした際に、画像ファイル名を自動的に説明、ALT、タイトル、キャプションに設定する * * - タイトル:画像のファイル名は自動的に画像のタイトルになります(WordPressの標準動作)。 * - キャプション:キャプションが空の場合、画像のファイル名をキャプションに設定します。 * - 説明:説明が空の場合、画像のファイル名を説明に設定します。 * - Altテキスト:Altテキストが空の場合、画像のファイル名をAltテキストに設定します。 */ add_action('add_attachment', function ($post_ID) { $image = \get_post($post_ID); $image_title = $image->post_title; // If there is no caption, set the filename as caption if (empty($image->post_excerpt)) { $image->post_excerpt = $image_title; // Caption } // If there is no description, set the filename as description if (empty($image->post_content)) { $image->post_content = $image_title; // Description } // Update Alt text if it’s empty if (!\get_post_meta($post_ID, '_wp_attachment_image_alt', true)) { \update_post_meta($post_ID, '_wp_attachment_image_alt', $image_title); // Alt text } // Update the image post \wp_update_post($image); }); |