-
Notifications
You must be signed in to change notification settings - Fork 5
/
be-automatic-alt-text.php
38 lines (32 loc) · 1.32 KB
/
be-automatic-alt-text.php
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
31
32
33
34
35
36
37
38
<?php
/**
* Plugin Name: BE Automatic Alt Text
* Plugin URI: https://github.com/billerickson/BE-Automatic-Alt-Text
* Description: Automatically adds alt text to images in Gutenberg block editor when you add the alt text in the Media Library
* Version: 1.0.0
* Author: Bill Erickson
* Author URI: https://www.billerickson.net
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License version 2, as published by the Free Software Foundation. You may NOT assume
* that you can use any other version of the GPL.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
add_filter( 'render_block', function( $content, $block ) {
if( 'core/image' !== $block['blockName'] )
return $content;
$alt = get_post_meta( $block['attrs']['id'], '_wp_attachment_image_alt', true );
if( empty( $alt ) )
return $content;
// Empty alt
if( false !== strpos( $content, 'alt=""' ) ) {
$content = str_replace( 'alt=""', 'alt="' . $alt . '"', $content );
// No alt
} elseif( false === strpos( $content, 'alt="' ) ) {
$content = str_replace( 'src="', 'alt="' . $alt . '" src="', $content );
}
return $content;
}, 10, 2 );