-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from prolific-digital/hot-fix-render-block-hook
Initial Release - Media Library Alt Text Updater Plugin
- Loading branch information
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
<?php | ||
/** | ||
* Plugin Name: Media Library Alt Text Updater | ||
* Plugin URI: https://prolificdigital.com | ||
* Description: A standalone plugin that updates image block alt text in posts and pages with the corresponding alt text from the media library. This plugin targets images that are missing alt text and ensures all images have descriptive alt text for better accessibility and SEO. | ||
* Version: 1.0.0 | ||
* Author: Prolific Digital | ||
* Author URI: https://prolificdigital.com | ||
* License: GPL-2.0+ | ||
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | ||
* Text Domain: media-library-alt-text-updater | ||
* Domain Path: /languages | ||
* Requires at least: 5.7 | ||
* Requires PHP: 7.3 | ||
*/ | ||
|
||
// Hook into the 'render_block' filter to modify the block content before rendering | ||
add_filter('render_block', 'media_library_alt_text_updater', 10, 2); | ||
|
||
/** | ||
* Updates the alt text of image blocks with the corresponding alt text from the media library. | ||
* | ||
* @param string $content The block content. | ||
* @param array $block The block details. | ||
* @return string The modified block content. | ||
*/ | ||
function media_library_alt_text_updater($content, $block) { | ||
// Check if the block is an image block | ||
if ('core/image' !== $block['blockName']) | ||
return $content; | ||
|
||
// Retrieve the alt text from the media library if the image ID is set | ||
if (isset($block['attrs']['id'])) { | ||
$alt = get_post_meta($block['attrs']['id'], '_wp_attachment_image_alt', true); | ||
} | ||
|
||
// If the alt text is empty, return the original content | ||
if (empty($alt)) | ||
return $content; | ||
|
||
// If the alt attribute is empty in the content, replace it with the alt text from the media library | ||
if (false !== strpos($content, 'alt=""')) { | ||
$content = str_replace('alt=""', 'alt="' . $alt . '"', $content); | ||
|
||
// If the alt attribute is missing, add it before the src attribute | ||
} elseif (false === strpos($content, 'alt="')) { | ||
$content = str_replace('src="', 'alt="' . $alt . '" src="', $content); | ||
} | ||
|
||
// Return the modified content | ||
return $content; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Media Library Alt Text Updater | ||
|
||
A standalone plugin that updates image block alt text in posts and pages with the corresponding alt text from the media library. This plugin targets images that are missing alt text and ensures all images have descriptive alt text for better accessibility and SEO. | ||
|
||
## Description | ||
|
||
### The Problem | ||
|
||
In WordPress, the alt text for images is an essential element for both accessibility and SEO. Alt text helps screen readers describe images to visually impaired users and provides context to search engines. However, the block editor (Gutenberg) introduced a challenge: it does not automatically pull in alt text from the media library for image blocks unless the alt text was already assigned before the image was added to the page. This differs from the classic editor, which did pull in the alt text from the media library by default. | ||
|
||
As a result, many image blocks may end up missing alt text if the content creator forgets to manually add it, leading to poor accessibility and SEO performance. This gap can be especially problematic for websites with a large number of images, making it cumbersome to manually update each image block with the correct alt text. | ||
|
||
### The Solution | ||
|
||
The Media Library Alt Text Updater plugin addresses this issue by automatically updating the alt text for image blocks in posts and pages using the alt text defined in the media library. This ensures that all images have the correct alt text, improving both the accessibility and SEO of your website. | ||
|
||
### Important Note | ||
|
||
This plugin will display the alt text for images on the frontend, but you will not see the alt text populate within the WordPress editor. This allows you the opportunity to override the alt text on an image-by-image basis directly within the editor if needed. | ||
|
||
## Features | ||
|
||
- **Automatic Alt Text Update:** Automatically updates image block alt text in posts and pages. | ||
- **Media Library Integration:** Uses alt text from the media library. | ||
- **Missing Alt Text Targeting:** Targets images that are missing alt text. | ||
- **Accessibility and SEO Improvement:** Improves website accessibility and SEO. | ||
- **Editor Override:** Allows for manual override of alt text on an image-by-image basis within the WordPress editor. | ||
|
||
## Installation | ||
|
||
1. **Upload the plugin files to the `/wp-content/plugins/media-library-alt-text-updater` directory**, or install the plugin through the WordPress plugins screen directly. | ||
2. **Activate the plugin** through the 'Plugins' screen in WordPress. | ||
3. The plugin will automatically start updating alt text for image blocks in posts and pages. | ||
|
||
## Usage | ||
|
||
Once the plugin is activated, it will automatically update the alt text for image blocks in your posts and pages with the alt text from the media library. No further configuration is necessary. | ||
|
||
## Frequently Asked Questions | ||
|
||
### Does this plugin update existing image blocks? | ||
|
||
Yes, the plugin updates existing image blocks in your posts, page, or custom psot type to ensure they have alt text from the media library. | ||
|
||
### What happens if an image in the media library does not have alt text? | ||
|
||
If an image in the media library does not have alt text, the plugin will not update the alt text for the corresponding image block. | ||
|
||
### Can I customize the alt text for individual image blocks? | ||
|
||
The plugin updates the alt text based on the media library. If you need to customize the alt text for individual image blocks, you should do so directly in the media library or override it within the WordPress editor. | ||
|
||
### Will the alt text be visible in the WordPress editor? | ||
|
||
No, the alt text updated by this plugin will not populate within the WordPress editor. This allows you to manually override the alt text on an image-by-image basis if needed. | ||
|
||
## Support | ||
|
||
For support, please contact [Prolific Digital](mailto:support@prolificdigital.com). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
=== AnimateWP === | ||
Contributors: millertchris | ||
Donate link: https://prolificdigital.com | ||
Tags: alt text, media, images, accessibility, seo | ||
Requires at least: 6.0 | ||
Tested up to: 6.6.1 | ||
Stable tag: 1.0.0 | ||
License: GPLv2 or later | ||
License URI: http://www.gnu.org/licenses/gpl-2.0.html | ||
A plugin that updates image block alt text using the media library's alt text, improving accessibility and SEO. |