<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog Management | Ecological Notes Web</title>
	<atom:link href="https://ecological-information.com/en/archives/category/blog-management/feed" rel="self" type="application/rss+xml" />
	<link>https://ecological-information.com/en</link>
	<description>A website where you can learn about the classification, distribution, and ecology of living things.</description>
	<lastBuildDate>Sun, 07 Jun 2026 08:12:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updateperiod>
	hourly	</sy:updateperiod>
	<sy:updatefrequency>
	1	</sy:updatefrequency>
	

<image>
	<url>https://ecological-information.com/wp-content/uploads/2024/07/ecological-information-favicon-150x150.jpg</url>
	<title>Blog Management | Ecological Notes Web</title>
	<link>https://ecological-information.com/en</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/>
<atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/>
<atom:link rel="hub" href="https://websubhub.com/hub"/>
<atom:link rel="self" href="https://ecological-information.com/en/archives/category/blog-management/feed"/>
	<item>
		<title>WordPress debug log (debug.log) bloat prevention! Settings and specifications of the automatic deletion plugin &quot;LH Clear Debug Log by Cron&quot;.</title>
		<link>https://ecological-information.com/en/archives/24055</link>
		
		<dc:creator><![CDATA[Ikeda, K.]]></dc:creator>
		<pubDate>Fri, 05 Jun 2026 04:28:00 +0000</pubDate>
				<category><![CDATA[ブログ運営]]></category>
		<guid ispermalink="false">https://ecological-information.com/?p=24055</guid>

					<description><![CDATA[Debug mode is a useful tool for identifying errors in WordPress site management and theme development. However, errors can sometimes occur […]]]></description>
										<content:encoded><![CDATA[<div class="wpulike wpulike-heart"><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button" aria-label="Like button" data-ulike-id="24055" data-ulike-nonce="a6f9faa5eb" data-ulike-type="post" data-ulike-template="wpulike-heart" data-ulike-display-likers="" data-ulike-likers-style="popover" class="wp_ulike_btn wp_ulike_put_image wp_post_btn_24055"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0"></span>			</div></div>
	
<p class="wp-block-paragraph">Debug mode is a useful tool for identifying errors when managing a WordPress site or developing themes. However, have you ever left the error log output enabled and suddenly found your server&#039;s storage capacity was being strained?</p>



<p class="wp-block-paragraph translation-block">Manually opening FTP and deleting or emptying <code>debug.log</code> file each time is a tedious and annoying task.</p>



<p class="wp-block-paragraph translation-block">Therefore, in this article, I will introduce &quot; <a rel="noopener" target="_blank" href="https://ja.wordpress.org/plugins/lh-clear-debug-log-by-cron">LH Clear Debug Log by Cron</a>,&quot; a plugin that completely automates the problem of bloated debug logs. It is extremely niche, with only &quot;10+&quot; active installations, but once configured, it is an excellent tool that intelligently manages logs in the background.</p>


<a rel="noopener" target="_blank" href="https://ja.wordpress.org/plugins/lh-clear-debug-log-by-cron" title="LH Clear Debug Log by Cron" class="blogcard-wrap external-blogcard-wrap a-wrap cf"><div class="blogcard external-blogcard eb-left cf"><div class="blogcard-label external-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail external-blogcard-thumbnail"><img decoding="async" src="https://ecological-information.com/wp-content/uploads/cocoon-resources/blog-card-cache/195473edf9a10ffbd2c0a434a4b6b130.jpg" alt="" class="blogcard-thumb-image external-blogcard-thumb-image" width="160" height="90" data-no-translation="" data-no-auto-translation=""></figure><div class="blogcard-content external-blogcard-content"><div class="blogcard-title external-blogcard-title">LH Clear Debug Log by Cron</div><div class="blogcard-snippet external-blogcard-snippet">A simple plugin that clears the debug log by the cron schedule when it gets too big.</div></div><div class="blogcard-footer external-blogcard-footer cf"><div class="blogcard-site external-blogcard-site"><div class="blogcard-favicon external-blogcard-favicon"><img decoding="async" src="https://www.google.com/s2/favicons?domain=https://ja.wordpress.org/plugins/lh-clear-debug-log-by-cron/" alt="ja.wordpress.org" class="blogcard-favicon-image external-blogcard-favicon-image" width="16" height="16" data-no-translation="" data-no-auto-translation=""></div><div class="blogcard-domain external-blogcard-domain">ja.wordpress.org</div></div></div></div></a>


<p class="wp-block-paragraph">This article provides a clear explanation of the plugin&#039;s detailed internal specifications and the results of testing it in the latest WordPress 7.0 environment. If you want to reduce the effort involved in log management and maintain optimal site performance, please refer to this article.</p>




  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-2" checked><label class="toc-title" for="toc-checkbox-2">table of contents</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">Causes and risks of WordPress debug log (debug.log) becoming excessively large</a></li><li><a href="#toc2" tabindex="0">Internal specifications and mechanism of the plugin &quot;LH Clear Debug Log by Cron&quot;</a><ol><li><a href="#toc3" tabindex="0">The threshold for automatic deletion is &quot;4MB&quot;.</a></li><li><a href="#toc4" tabindex="0">Customizing capacity limits with filter hooks</a></li><li><a href="#toc5" tabindex="0">Background processing using WP-Cron</a></li></ol></li><li><a href="#toc6" tabindex="0">[Operation Verification] Even with an official version that hasn&#039;t been updated in 4 years, normal operation has been confirmed in the latest WordPress 7.0 environment.</a></li><li><a href="#toc7" tabindex="0">Summary: A lightweight solution that frees you from manually deleting error logs.</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">Causes and risks of WordPress debug log (debug.log) becoming excessively large</span></h2>



<p class="wp-block-paragraph translation-block">WordPress&#039;s debug mode (<code>WP_DEBUG_LOG</code>) is useful for troubleshooting and customizing bugs. However, if you continue to run your WordPress site with this mode enabled, errors and Notices will accumulate, and <strong>the log file can grow to several gigabytes in size</strong>.</p>



<p class="wp-block-paragraph">If log files become too large, they risk causing the following problems:</p>



<ul class="wp-block-list">
<li>The server&#039;s disk space is being consumed, causing the site to go down.</li>



<li>The server&#039;s I/O load increases, slowing down the website&#039;s loading speed.</li>
</ul>



<p class="wp-block-paragraph">Log problems often become irreversible by the time you notice them, and manually opening FTP and deleting them each time is inefficient.</p>



<h2 class="wp-block-heading"><span id="toc2">Internal specifications and mechanism of the plugin &quot;LH Clear Debug Log by Cron&quot;</span></h2>



<p class="wp-block-paragraph translation-block">&quot; <a rel="noopener" target="_blank" href="https://ja.wordpress.org/plugins/lh-clear-debug-log-by-cron/#description">LH Clear Debug Log by Cron</a> &quot; is a very niche plugin, with only &quot;10+&quot; active installations, but it&#039;s a practical plugin specifically designed for automatic log cleanup. The internal specifications, as published in the official FAQ, are as follows:</p>



<h3 class="wp-block-heading"><span id="toc3">The threshold for automatic deletion is &quot;4MB&quot;.</span></h3>



<p class="wp-block-paragraph">By default, the log file is automatically cleared (emptied) when it exceeds 4,194,304 bytes (approximately 4 MB). This ensures that the file is reset in increments of a few MB before it reaches several GB, preventing it from overwhelming the server.</p>



<h3 class="wp-block-heading"><span id="toc4">Customizing capacity limits with filter hooks</span></h3>



<p class="wp-block-paragraph translation-block"><strong>By using the filter hooks provided within the plugin, you can freely change the size threshold (4MB) for deletion</strong>. You can adjust it from the code side to suit your site&#039;s operating environment, such as &quot;delete at 2MB&quot; or &quot;allow up to 10MB&quot;.</p>



<h3 class="wp-block-heading"><span id="toc5">Background processing using WP-Cron</span></h3>



<p class="wp-block-paragraph">We will use WordPress&#039;s scheduled execution system (WP-Cron) to periodically monitor and delete log files based on their size.</p>



<div class="wp-block-cocoon-blocks-icon-box common-icon-box block-box information-box">
<p class="wp-block-paragraph translation-block"><strong>Note: WP-Cron must be working.</strong> This plugin relies on WP-Cron, so automatic deletion will not work in environments where WP-Cron is intentionally disabled on the server side. Please check your Cron settings before installation.</p>
</div>



<h2 class="wp-block-heading"><span id="toc6">[Operation Verification] Even with an official version that hasn&#039;t been updated in 4 years, normal operation has been confirmed in the latest WordPress 7.0 environment.</span></h2>



<p class="wp-block-paragraph">The official metadata states &quot;Last updated: 4 years ago&quot; and &quot;Verified latest version: 6.0.12,&quot; which at first glance raises concerns about its suitability for installation.</p>



<p class="wp-block-paragraph translation-block">However, <strong>we have verified and confirmed that it is properly automatically deleted (cleared) when the debug log exceeds 4MB in an actual &quot;WordPress 7.0&quot; environment</strong>.</p>



<p class="wp-block-paragraph">Because it has an extremely simple code design with a single function (single task) and no administration panel (UI) or unnecessary settings pages, it is less affected by major WordPress updates and works stably even with the current version.</p>



<h2 class="wp-block-heading"><span id="toc7">Summary: A lightweight solution that frees you from manually deleting error logs.</span></h2>



<p class="wp-block-paragraph translation-block">While it&#039;s not a flashy tool for everyone, &quot; <a rel="noopener" target="_blank" href="https://ja.wordpress.org/plugins/lh-clear-debug-log-by-cron/#description">LH Clear</a> Debug Log by Cron&quot; is a very reliable option for administrators who &quot;keep the development environment in debug mode at all times&quot; or &quot;want to monitor logs in the production environment while also imposing capacity limits.&quot;</p>



<p class="wp-block-paragraph">To be honest, you can achieve the same result by pasting the following PHP code into function.php or the &quot;Code Snippets&quot; plugin, and then setting the &quot;WP Crontrol&quot; plugin to trigger delete_debug_log_daily once a week.</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-php translation-block" data-lang="PHP"><code>//wp-content/debug.logを一定期間で自動削除する
add_action('delete_debug_log_daily', function () {
    $file = WP_CONTENT_DIR . '/debug.log';
    if (file_exists($file)) {
        unlink($file);
    }
});</code></pre></div>



<p class="wp-block-paragraph">However, personally, I&#039;ve learned the hard way that the more PHP code I add, the more debugging and refactoring I have to do, which is a real hassle, so I want to get help from others as much as possible.</p>



<p class="wp-block-paragraph">With its clear specification of &quot;automatic clearing at 4MB&quot; and proven performance in the current version, those who want to automate log bloat prevention should definitely consider implementing it.</p>



<p class="wp-block-paragraph translation-block">If you want to manage debug logs comprehensively, including automatic deletion, there is a plugin called &quot; <a rel="noopener" target="_blank" href="https://ja.wordpress.org/plugins/debug-log-viewer/">Debug Log Viewer</a>,&quot; but the free version is quite large at a fixed 50MB. The Pro version allows you to change the size between 1 and 5000MB.</p>


<a rel="noopener" target="_blank" href="https://ja.wordpress.org/plugins/debug-log-viewer" title="Debug Log Viewer" class="blogcard-wrap external-blogcard-wrap a-wrap cf"><div class="blogcard external-blogcard eb-left cf"><div class="blogcard-label external-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail external-blogcard-thumbnail"><img decoding="async" src="https://ecological-information.com/wp-content/uploads/cocoon-resources/blog-card-cache/cbaf752b69ea4ba81615a008d8376d18.jpg" alt="" class="blogcard-thumb-image external-blogcard-thumb-image" width="160" height="90" data-no-translation="" data-no-auto-translation=""></figure><div class="blogcard-content external-blogcard-content"><div class="blogcard-title external-blogcard-title">Debug Log Viewer</div><div class="blogcard-snippet external-blogcard-snippet">Effortlessly view, search, filter and manage your WordPress debug.log in the admin dashboard. Real-time monitoring and e…</div></div><div class="blogcard-footer external-blogcard-footer cf"><div class="blogcard-site external-blogcard-site"><div class="blogcard-favicon external-blogcard-favicon"><img loading="lazy" decoding="async" src="https://www.google.com/s2/favicons?domain=https://ja.wordpress.org/plugins/debug-log-viewer/" alt="ja.wordpress.org" class="blogcard-favicon-image external-blogcard-favicon-image" width="16" height="16" data-no-translation="" data-no-auto-translation=""></div><div class="blogcard-domain external-blogcard-domain">ja.wordpress.org</div></div></div></div></a>		<div class="wpulike wpulike-heart"><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button" aria-label="Like button" data-ulike-id="24055" data-ulike-nonce="a6f9faa5eb" data-ulike-type="post" data-ulike-template="wpulike-heart" data-ulike-display-likers="" data-ulike-likers-style="popover" class="wp_ulike_btn wp_ulike_put_image wp_post_btn_24055"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0"></span>			</div></div>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>This article explains how to retrieve the text of an image caption and assign it to the Alt attribute (alternative text) using JavaScript and PHP (using ChatGPT).</title>
		<link>https://ecological-information.com/en/archives/11010</link>
		
		<dc:creator><![CDATA[Ikeda, K.]]></dc:creator>
		<pubDate>Mon, 18 Dec 2023 05:19:40 +0000</pubDate>
				<category><![CDATA[ブログ運営]]></category>
		<category><![CDATA[ウェブ開発]]></category>
		<category><![CDATA[クライアントサイド開発]]></category>
		<category><![CDATA[フロントエンド開発]]></category>
		<category><![CDATA[プログラミング]]></category>
		<category><![CDATA[生態]]></category>
		<guid ispermalink="false">https://ecological-information.com/?p=11010</guid>

					<description><![CDATA[When you&#039;re doing web development or running a website, you might be wondering, &quot;When adding a caption to an image, do I also need to add an Alt attribute (alternative text)?&quot;]]></description>
										<content:encoded><![CDATA[<div class="wpulike wpulike-heart"><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button" aria-label="Like button" data-ulike-id="11010" data-ulike-nonce="04f4769fef" data-ulike-type="post" data-ulike-template="wpulike-heart" data-ulike-display-likers="" data-ulike-likers-style="popover" class="wp_ulike_btn wp_ulike_put_image wp_post_btn_11010"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0"></span>			</div></div>
	
<p class="wp-block-paragraph">Have you ever thought, while developing or managing a website, &quot;Wouldn&#039;t it be great if I could specify an Alt attribute (alternative text) at the same time as adding a caption to an image?&quot; Generally, captions are for human users, while Alt attributes are for crawlers (machines like Googlebot). Having both improves user experience and SEO, and can potentially increase website traffic. I&#039;ve long found this cumbersome, so I created a JavaScript script to address this. Please feel free to use it as a reference.</p>




  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-4" checked><label class="toc-title" for="toc-checkbox-4">table of contents</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">Solve your WordPress image management challenges! How to set captions and alt attributes in bulk using ChatGPT.</a></li><li><a href="#toc2" tabindex="0">What are captions and alt attributes? What&#039;s the difference between them? Why are both necessary?</a></li><li><a href="#toc3" tabindex="0">How to implement JavaScript in WordPress</a></li><li><a href="#toc4" tabindex="0">How can I check if the Alt attribute is specified correctly?</a></li><li><a href="#toc5" tabindex="0">What are the advantages and disadvantages of this method?</a></li><li><a href="#toc6" tabindex="0">How to add the content of a figcaption to the Alt attribute in PHP (Added June 2, 2026)</a></li><li><a href="#toc7" tabindex="0">References</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">Solve your WordPress image management challenges! How to set captions and alt attributes in bulk using ChatGPT.</span></h2>



<p class="wp-block-paragraph">Have you ever thought, &quot;Wouldn&#039;t it be great if I could specify the Alt attribute (alternative text) at the same time as adding captions to images on static sites or CMSs like WordPress?&quot;</p>



<p class="wp-block-paragraph">My blog often features explanations about living creatures, and I frequently include ecological photographs. While WordPress makes it easy to specify image captions in the main part of the editing screen, the Alt attribute must be selected and specified from the sidebar on the right side of the editing screen. When inserting a large number of images, it&#039;s difficult to see the content of the Alt attribute at a glance, leading to frequent oversights about whether it&#039;s been specified correctly.</p>



<p class="wp-block-paragraph">Since captions and alt attributes are almost always the same, I was wondering, &quot;Isn&#039;t there a plugin that makes them the same?&quot; and started searching for one.</p>



<p class="wp-block-paragraph">However, I couldn&#039;t find a suitable solution, and although I could generate it using JavaScript, I lacked the technical skills at the time, so I left this issue unresolved for a long time. Recently, however, I&#039;ve seriously studied HTML, CSS, and JavaScript, and with the advancements in generation AI, I&#039;ve discovered that it can be solved using ChatGPT and JavaScript, so I&#039;m sharing it here.</p>



<h2 class="wp-block-heading"><span id="toc2">What are captions and alt attributes? What&#039;s the difference between them? Why are both necessary?</span></h2>



<p class="wp-block-paragraph">First, let me explain why it&#039;s necessary (or considered necessary) to specify both the caption and alt attributes.</p>



<p class="wp-block-paragraph">Captions are primarily used to provide descriptions and supplementary information about images. Their main role is to provide context to the user and aid in understanding.</p>



<p class="wp-block-paragraph translation-block">In HTML, captions are usually<code><figure></code> It is placed inside another element, and that element is used within that other element. </p>



<p class="wp-block-paragraph">On the other hand, the original purpose of the Alt attribute is to provide alternative text in case the image is read aloud or cannot be displayed.</p>



<p class="wp-block-paragraph">The original function of the Alt attribute was to provide alternative information for an image when the image itself cannot be displayed (Craven, 2006), but it is also used to help visually impaired people understand images through sound (Bigham et al., 2010).</p>



<p class="wp-block-paragraph translation-block">The Alt attribute in HTML<code><img></code> It is specified as an &quot;attribute&quot; of the element.</p>



<p class="wp-block-paragraph">The HTML source code has the following structure:</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-html translation-block" data-lang="HTML"><code><main><figure><img src="sample.jpg" alt="sample" /><!--altで指定された部分がAlt属性--><figcaption>sample</figcaption><!--figcaptionで囲まれた部分がキャプション--></figure></main></code></pre></div>



<p class="wp-block-paragraph">However, this alone might not explain why it&#039;s necessary to specify both the caption and alt attributes.</p>



<p class="wp-block-paragraph translation-block"><strong>This is because the content of the Alt attribute is now used as part of search engine optimization (SEO) and is considered to potentially influence search results.</strong></p>



<p class="wp-block-paragraph translation-block">Search engines like <cite>Google Search</cite> use crawlers such as Googlebot to programmatically collect information from the web. Research has shown that <cite>Google Search</cite> uses these crawlers to reference Alt attributes, which are then reflected in the search results (Ferraz, 2015).</p>



<p class="wp-block-paragraph">On the other hand, captions are not considered to have much of an impact and are seen as playing a role in improving the user experience by making it easier for site visitors to read articles.</p>



<p class="wp-block-paragraph">In short, in modern times, captions are for the user (human) who is viewing the content, while alt attributes are for crawlers (machines like Googlebot) that are roaming the site.</p>



<p class="wp-block-paragraph translation-block"><strong><span class="marker-under-red">In other words, by specifying the Alt attribute, image search sites such as <cite>Google Image Search</cite> may be able to display your website&#039;s images more accurately.</span></strong></p>



<p class="wp-block-paragraph">Therefore, it is generally considered beneficial to specify both options. It also contributes to improved accessibility.</p>



<p class="wp-block-paragraph">However, there are many uncertainties about whether crawlers actually ignore captions, and I haven&#039;t seen any articles that verify this, so personally, I can&#039;t say for sure that the Alt attribute has any influence.</p>



<p class="wp-block-paragraph translation-block">Furthermore, as of the time of the aforementioned paper, it was found that <cite>Yahoo!,</cite> <cite>Bing</cite>, and <cite>DuckDuckGo</cite> do not refer to the Alt attribute (Ferraz, 2015).</p>



<h2 class="wp-block-heading"><span id="toc3">How to implement JavaScript in WordPress</span></h2>



<p class="wp-block-paragraph">However, if there&#039;s even the slightest impact, it&#039;s preferable to specify both options with minimal effort.</p>



<p class="wp-block-paragraph">Unfortunately, my knowledge of JavaScript&#039;s DOM (Document Object Model) is still limited, making it difficult to write the code from scratch. Therefore, I decided to create it while making modifications along with ChatGPT 3.5.</p>



<p class="wp-block-paragraph">The following code was obtained:</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-js translation-block" data-lang="JavaScript"><code>var images = document.querySelectorAll('figure img');
images.forEach(function (img) {
  if (!img.alt) {
    var figcaption = img.parentElement.querySelector('figcaption');
    if (figcaption) {
      img.alt = figcaption.textContent || figcaption.innerText;
    }
  }
});</code></pre></div>



<p class="wp-block-paragraph">The content is surprisingly simple, so I think it&#039;s highly versatile.</p>



<p class="wp-block-paragraph">In Japanese, the following operations are being performed:</p>



<ol class="wp-block-list">
<li class="translation-block">This function retrieves all <code>img</code> elements from all <code>figure</code> ` elements on the page. The retrieved elements are stored in the <code>images</code> variable as a NodeList object.</li>



<li class="translation-block">The <code>forEach</code> method is used on the <code>images</code> variable to execute the specified function for each element.</li>



<li class="translation-block">The code performs branching processing if the Alt attribute is not present for each <code>img</code> element.</li>



<li class="translation-block"><code>parentElement</code> and <code>querySelector</code> methods are used on the <code>img</code> element to obtain the <code>figcaption</code> element, which is the parent element of the `&lt;img&gt;` element, and the first element found within it is stored in the <code>figcaption</code> variable.</li>



<li class="translation-block">This process is executed if <code>figcaption</code> variable exists.</li>



<li class="translation-block">The Alt attribute of <code>img</code> element is set to the text content of the <code>figcaption</code> element. If <code>textContent</code> exists, it is used; otherwise, <code>innerText</code> is used. This is because some browsers only support one of them.</li>
</ol>



<p class="wp-block-paragraph">In the case of WordPress, you can make this script work by going to &quot;Appearance&quot; → &quot;Theme File Editor&quot; in the menu bar on the left side of the editing screen, and pasting it into &quot;javascript.js&quot; under the heading &quot;Theme Files&quot;.</p>



<p class="wp-block-paragraph">Note that if you are using a CMS (Content Management System) such as WordPress, the HTML code will be enormous, so the id attribute containing the main body will be as follows:<main> It&#039;s faster to limit the processing to items included in the specified category.</p>



<p class="wp-block-paragraph translation-block">There are two types of selectors: <code>querySelectorAll()</code> and <code>getElementById()</code>. However, when specifying a single ID, using <code>getElementById()</code> is faster.</p>



<p class="wp-block-paragraph translation-block">Furthermore, the following code uses <code>document.addEventListener</code> method to wait until the document is fully loaded before executing the script.</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-js translation-block" data-lang="JavaScript"><code>document.addEventListener('DOMContentLoaded', function () {
  var main = document.getElementById('main');
  if (main) {
    var images = main.querySelectorAll('figure img');
    images.forEach(function (img) {
      if (!img.alt) {
        var figcaption = img.parentElement.querySelector('figcaption');
        if (figcaption) {
          img.alt = figcaption.textContent || figcaption.innerText;
        }
      }
    });
  }
});</code></pre></div>



<h2 class="wp-block-heading"><span id="toc4">How can I check if the Alt attribute is specified correctly?</span></h2>



<p class="wp-block-paragraph"><span class="marker-under-red"><strong>Whether the Alt attribute is specified correctly is...<cite>Google Search Console</cite>You can find out by checking.</strong></span></p>



<p class="wp-block-paragraph translation-block">Please note that in order to use <cite>Google Search Console,</cite> you need to register your website&#039;s URL in advance and ensure that your published articles have been crawled.</p>



<p class="wp-block-paragraph">If registered, paste the URL of the article you want to check into the search form labeled &quot;Inspect all URLs in ~&quot; and perform the search.</p>



<p class="wp-block-paragraph">After searching, clicking on &quot;Crawled Pages&quot; on the displayed page will show the HTML recognized by Googlebot on the right side.</p>



<p class="wp-block-paragraph translation-block">Click once within the code on the right, then press <kbd>Ctrl + F</kbd> on your keyboard to open the search box. Searching for &quot;alt=&quot; in this box will allow you to see the alt attribute specification for the image.</p>



<p class="wp-block-paragraph">If the above script is working correctly, text should be added to the alt attribute.</p>



<h2 class="wp-block-heading"><span id="toc5">What are the advantages and disadvantages of this method?</span></h2>



<p class="wp-block-paragraph">One of the advantages of this method is, of course, that when you enter text in the image caption, the alt attribute is automatically assigned, just as intended.</p>



<p class="wp-block-paragraph">As of version 6.4.2, WordPress automatically inserts the &quot;Title&quot; property of the image when you add it using the visual editor. Therefore, if you specify a title in the property, it should be possible to automatically fill in everything. However, I haven&#039;t done this because I use this property for a different purpose.</p>



<p class="wp-block-paragraph">Another side effect I noticed was that I developed the habit of properly adding text to image captions.</p>



<p class="wp-block-paragraph">Up until now, I often carelessly pasted images onto my sites, thinking, &quot;If there&#039;s an image, visitors will understand, and adding captions is a hassle, so it&#039;s fine.&quot; However, now that I know there&#039;s also the benefit of improved SEO, I&#039;ve developed the habit of entering information much more seriously and accurately.</p>



<p class="wp-block-paragraph">I don&#039;t think there are any major drawbacks, but because it dynamically generates content using JavaScript, if the crawler doesn&#039;t execute the JavaScript or the execution is too slow, it might not be able to recognize it properly, and therefore the specification might not be successful. Be careful if your site is heavy due to heavy use of JavaScript.</p>



<p class="wp-block-paragraph translation-block"><strong>The script above prioritizes the Alt attribute if one is already entered, so even if you later want to change the caption and Alt attribute to different content, it will be reflected without any problems.</strong> Therefore, if you want to specify the Alt attribute individually, specify it from the right sidebar of the visual editor as described above.</p>



<p class="wp-block-paragraph">If you find entering Alt attributes tedious, you should definitely give this a try!</p>



<h2 class="wp-block-heading"><span id="toc6">How to add the content of a figcaption to the Alt attribute in PHP (Added June 2, 2026)</span></h2>



<p class="wp-block-paragraph">I previously used the JavaScript described above, but now that I understand WordPress better, I no longer use it.</p>



<p class="wp-block-paragraph">We&#039;ve minimized the use of JavaScript because we know it has a significant negative impact on page speed.</p>



<p class="wp-block-paragraph">Currently, this process is performed using the following PHP code. Those who wish to do the same thing in the future should use the following code.</p>



<p class="wp-block-paragraph translation-block">Note that <code>function_exists(&#039;trp_translate&#039;)</code> is unnecessary for sites that do not use the &quot;TranslatePress&quot; automatic translation WordPress plugin. Conversely, for sites that do use TranslatePress, this is excellent code because it adds Alt attributes to images in the language of each page.</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-php translation-block" data-lang="PHP"><code>add_action('template_redirect', function () {

    ob_start(function ($html) {

        /**
         * figcaption または 空alt が無ければ即return
         */
        if (
            strpos($html, '&lt;figcaption&#039;) === false ||
            strpos($html, &#039;alt=&quot;&quot;&#039;) === false
        ) {
            return $html;
        }

        return preg_replace_callback(
            &#039;/<figure>]*&gt;.*?/isu',
            function ($m) {

                $figure = $m[0];

                /**
                 * 空altが無ければスキップ
                 */
                if (strpos($figure, 'alt=""') === false) {
                    return $figure;
                }

                /**
                 * figcaption取得
                 */
                if (
                    !preg_match(
                        '/<figcaption>]*&gt;(.*?)/isu',
                        $figure,
                        $cap
                    )
                ) {
                    return $figure;
                }

                $caption = wp_strip_all_tags($cap[1]);

                /**
                 * 区切り前のみ
                 */
                $caption = preg_split('/[｜|]/u', $caption)[0];

                /**
                 * TranslatePressで現在言語へ翻訳
                 */
                if (function_exists('trp_translate')) {

                    $caption = trp_translate(
                        $caption,
                        null,
                        false
                    );
                }

                $caption = esc_attr(trim($caption));

                /**
                 * alt置換
                 */
                return preg_replace(
                    '/alt=(["\'])\s*\1/isu',
                    'alt="' . $caption . '"',
                    $figure,
                    1
                );

            },
            $html
        );

    });

});</code></pre></div>



<h2 class="wp-block-heading"><span id="toc7">References</span></h2>



<p class="wp-block-paragraph" itemscope="" itemtype="https://schema.org/Book" itemprop="citation">Bigham, JP, Brudvik, JT, &amp; Zhang, B. (2010). Accessibility by demonstration: enabling end users to guide developers to web accessibility solutions. In A. Barreto, &amp; VL Hanson (Eds.), <cite itemprop="name">Proceedings of the 12th international ACM SIGACCESS conference on Computers and accessibility</cite> (pp. 35-42). Association for Computing Machinery. ISBN: 9781605588810, <a rel="noopener" target="_blank" href="https://doi.org/10.1145/1878803.1878812">https://doi.org/10.1145/1878803.1878812</a></p>



<p class="wp-block-paragraph translation-block" itemscope="" itemtype="https://schema.org/ScholarlyArticle" itemprop="citation">Craven, TC (2006). Some Features of “Alt” Texts Associated with Images in Web Pages. <cite>Information Research: An International Electronic Journal</cite>, <em data-no-auto-translation="">11</em> (2), 250. ISSN: 1368-1613, <a rel="noopener" target="_blank" href="https://informationr.net/ir/11-2/paper250.html">https://informationr.net/ir/11-2/paper250.html</a></p>



<p class="wp-block-paragraph translation-block" itemscope="" itemtype="https://schema.org/ScholarlyArticle" itemprop="citation">Ferraz, R. (2015). Exploring web attributes related to image accessibility and their impact on search engine indexing. <cite>Procedia Computer Dcience</cite>, <em data-no-auto-translation="">67</em>, 171-184. <a rel="noopener" target="_blank" href="https://doi.org/10.1016/j.procs.2015.09.261">https://doi.org/10.1016/j.procs.2015.09.261</a></p>
		<div class="wpulike wpulike-heart"><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button" aria-label="Like button" data-ulike-id="11010" data-ulike-nonce="04f4769fef" data-ulike-type="post" data-ulike-template="wpulike-heart" data-ulike-display-likers="" data-ulike-likers-style="popover" class="wp_ulike_btn wp_ulike_put_image wp_post_btn_11010"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0"></span>			</div></div>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>