Skip to content

Commit

Permalink
Added functionality to update captions
Browse files Browse the repository at this point in the history
* Updated alt replacement with preg_replace too
  • Loading branch information
DasBen committed Mar 12, 2022
1 parent e89978f commit 6312b22
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/.idea/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# IntelliJ
.idea
43 changes: 18 additions & 25 deletions casualben-automatic-alt-and-caption-text.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* Description: Automatically adds alt text to images in Gutenberg block editor when you add the alt text in the Media Library
* Version: 1.1.0
* Author: Benjamin Pahl - CasualBen
* Author URI: https://www.casualben.com
* Author URI: https://www.casual-ben.com
*
* Including Version 1.0.0: BE Automatic Alt Text - Bill Erickson - https://github.com/billerickson/BE-Automatic-Alt-Text
*
*
* 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.
Expand All @@ -18,28 +18,21 @@
*
*/

add_filter( 'render_block', function( $content, $block ) {

if( 'core/image' == $block['blockName'] ) {
// Update Alt Text
$alt = get_post_meta( $block['attrs']['id'], '_wp_attachment_image_alt', true );
if( !empty( $alt ) ) {
// Empty alt
if( false !== strpos( $content, 'alt=""' ) ) {
$content = str_replace( 'alt=""', 'alt="' . $alt . '"', $content );
add_filter('render_block', function ($content, $block) {

if ('core/image' == $block['blockName']) {
// Update Alt Text
$alt = get_post_meta($block['attrs']['id'], '_wp_attachment_image_alt', true);
if (!empty($alt)) {
$content = preg_replace('/alt=".*"/', 'alt="' . $alt . '"', $content);
}

// No alt
} elseif( false === strpos( $content, 'alt="' ) ) {
$content = str_replace( 'src="', 'alt="' . $alt . '" src="', $content );
}
}

// Update Caption
$caption = wp_get_attachment_caption( $block['attrs']['id']);
if( !empty( $alt ) ) {
$content = str_replace( '<figcaption>.*</figcaption>', '<figcaption>'.$caption.'</figcaption>', $content );
}
}
// Update Caption
$caption = wp_get_attachment_caption($block['attrs']['id']);
if (!empty($caption)) {
$content = preg_replace('/<figcaption>.*<\/figcaption>/', '<figcaption>' . $caption . '</figcaption>', $content);
}
}

return $content;
}, 10, 2 );
return $content;
}, 10, 2);

0 comments on commit 6312b22

Please sign in to comment.