I found the problem. It was updating the post instead of the attachment. So instead of $post->ID, I create another string that stores the image ID and replace that with post->ID and it works.
function post_extra_save( $post_id, $post){
if ( has_blocks( $post->post_content ) ) {
$blocks = parse_blocks( $post->post_content );
foreach ( $blocks as $block ) {
if ( $block['blockName'] === 'acf/opby-cover-image' ) { // name of block
$media_url_thumb = $block['attrs']['data']['image_post']; // Image ID from the block
$cover_img_photo_alt = $block['attrs']['data']['photo_alt_text']; // text field for `photo_alt_text`
update_post_meta($media_url_thumb, '_wp_attachment_image_alt', $cover_img_photo_alt);
}
}
};
}
add_action( 'save_post', 'post_extra_save', 10, 2 );
Related Posts:
- remove links from images using functions.php
- How to add classes to images based on their categories?
- Auto delete WordPress images/thumbnails (all sizes) and featured after X days/hours, or similar?
- How to add a rel attribute to images that contains their categories?
- Best way to programatically add “rel” attributes to page and post images
- Image rotation fails to regenerate custom sized thumbnail
- Display images that are not in the content
- How to hide image-url if no attachment?
- What are the meta fields for an attachment?
- Different image using srcset function
- Auto delete content in specific folder inside media library
- Different body image backgrounds on different pages, posts and categories
- Open image size links in a page instead of direct image link
- Missing feature image link function
- Add image size if page template
- Set JPEG compression for specific custom image sizes
- Issue with wp_get_attachment_image() and SVG file type
- How to change the markup WordPress inserts for post images
- functions.php inject inline css
- Programmatically Set First Image as Featured
- Removing default image size list in Media Box
- WP 4.4. responsive loads normal image after loading the responsive image
- How to create thumbnails for PDF uploads?
- Show/hide Widgets in Dashboard Based on Current Advanced Custom Fields Option
- Set image size on media and text block with a function
- Why are image thumbnails cropped proportionally (not per dimensions)?
- Gallery images titles – get from post
- Remove images from get_the_excerpt
- Why are image thumbnails cropped proportionally (not per dimensions)?
- What is the “icon” parameter in wp_get_attachment_image_src used for?
- How to use max and min values of custom fields
- Overwrite Parent Theme add_image_size in Child Theme
- Images uploaded using media uploader are appearing upside down
- Count total number of images in post and echo results as number
- Can’t check if a post has thumbnail adding filter to get_post_metadata()
- Automatically wrap multiple images in div
- Retrieve tags data in post body
- Run function when WordPress new multisite is created or ACF field is updated
- Understanding and altering the structure of posted images
- Overwriting Parent Theme’s Image Sizes
- Load post attached images on a single page site with fancybox
- Publish/Update post is changing image links from file url to post url
- Featured Image Thumbnail Creation
- show first 3 thumbnails of posts in different sizes [closed]
- Getting custom image size URL in functions
- Set spesific size of featured images
- media resize/crop possibilities?
- Get ID of first image attached to a post
- ACF not showing
- Way to store featured image width and height in two separate variables?
- srcset and custom field get_post_meta and wp_get_attachment_url
- Limit dimensions of “Full” size images
- Visual Composer creating own shortcodes with vc_map() to return simple Image
- attachment page template? only show attachments for current post?
- Access category within rss2_head hook?
- Can set_post_thumbnail be used to remove a thumbnail?
- Compare meta key to current date in pre get post
- Woocommerce image sizes missing from Appearance › Customize but not declared by theme
- Need To Change WordPress Meta Tags… Includes wordpress shortcodes
- Need to convert image url to a Base_64 data url with wordpress function..
- How to add a new image size and apply it to posts only?
- Custom Image Size in Insert Media
- Perform function on publish AND save (not just save)
- Using add_image_size in functions.php to get original uploaded file
- get_image_tag() html output : empty src attribute
- Need To Resize Images Exactly Without Losing Image Details
- Custom image sizes showing in Classic Editor only when upladed directly to post
- Override a Post’s URL with Advanced Custom Fields Function
- how can I change all wordpress media file url to custom cdn url?
- How to display an image before title text in menu items
- How to change images url in function.php?
- WP_Query for Attachments not working as expected
- Class parameter in get_avatar args doesn’t get added to output
- Help with with my function for wordpress
- Get list of posts from attachment
- Images with overlay
- ACF Date Form in Custom Admin Field
- Remove all video attachment, both files and post/postmeta from database?
- Comment function for page
- ACF Filter return value
- Printing medium sized image
- Why am I losing image quality on my site?
- Replace image attributes for lazyload plugin on image elements with specific class
- How to delete attachments associated with custom field type when post property changes? [closed]
- Zip all (original) images from media gallery
- No compression occurs on my jpegs after adding jpeg_quality hook to my functions.php file
- Update add_image_size
- Function the_content
- Set thumbnail from URL, by grabbing image in functions.php
- Dynamically adding Captions to images
- Full size image option removing problem
- Image width issue in IE [closed]
- image_size with respect to aspect ratios
- How to resize image from import image url?
- How to crop image in WordPress with aspect ratio for any size of image
- How to add height and width dimensions automatically to archive descriptions
- Update post meta with wp cron
- How to overwrite image if it already exists – WordPress, Gravity form
- How Do I Prevent Featured Image Caption Displaying Above And Below Image
- How to force Cover block to use the full size image resolution