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
- warning: trying to access array offset on value of type bool ACF field image group
- attachment page template? only show attachments for current post?
- Add php code to wp_print_scripts?
- How to get an attachment id from a filename
- Access category within rss2_head hook?
- Can set_post_thumbnail be used to remove a thumbnail?
- Programmatically add Yoast meta description after post save
- Compare meta key to current date in pre get post
- Woocommerce image sizes missing from Appearance › Customize but not declared by theme
- How to properly set custom logo size?
- Add attribute only to first image of every post via functions.php
- 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 Shortcode to html img code?
- How to add a new image size and apply it to posts only?
- Remove attachment page for audio media type only
- Function to insert missing image size attributes into img tags
- Get Attachment Category Name
- Image Crop Not Working
- Using two loops in one WordPress template
- Use existing image sizes for WooCommerce
- Problem in using Customizer
- Insert a field with PREG_REPLACE – strange behaviour
- wp_get_attachment_image_src returns image on main domain, false on subdomain
- Retrieve First Image Function
- Custom Image Size in Insert Media
- Perform function on publish AND save (not just save)
- Featured Image keeps cropping
- Prevent creating multiple image resizes in twentytwelve template
- Conditional function for excluding first image from content, not working
- Using add_image_size in functions.php to get original uploaded file
- get_image_tag() html output : empty src attribute
- How d0 i get the number of attachments in the post
- Need To Resize Images Exactly Without Losing Image Details
- Display ACF category image on archive and single template files
- Featured Image and Tags problem
- 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?
- WordPress Function Assistance with Loop
- How to show ACF value under post/page title in wp-admin
- How to display an image before title text in menu items
- Is it possible to limit number of files a non-admin user can upload via the WordPress media library?
- How to change images url in function.php?
- Filter works on last selection but no others
- WP_Query for Attachments not working as expected
- Class parameter in get_avatar args doesn’t get added to output
- large image size of 1440×1440 not pulling 2x of 2880×2880 image
- Help with with my function for wordpress
- Can You Set A Minimum Image Dimension For Resizing Images?
- blank page with wp_get_attachment
- Get list of posts from attachment
- (Divi): How to make WordPress load images of specific size for Blurb modules on given page with a funciton?
- Images with overlay
- ACF Date Form in Custom Admin Field
- Assign IDs to headings in ACF using functions.php
- Reference multiple style sheets, clearing styles for permalink page, custom fields for css
- get_field() with ID in one signal filter not working
- Remove all video attachment, both files and post/postmeta from database?
- Echo a String Based On Geolocation? [Woocommerce || ACF]
- Featured image on archive page based on post type
- Comment function for page
- ACF Filter return value
- Printing medium sized image
- restrict uploaded image size and fixed image display size
- Why am I losing image quality on my site?
- Replace image attributes for lazyload plugin on image elements with specific class
- Using Switch Statement to Change Image According to Last Digit of Topic ID
- Disable links to images only if link is an image
- Restricted images only for logged user. (hidden url)
- How to delete attachments associated with custom field type when post property changes? [closed]
- Return ACF Field value function
- Zip all (original) images from media gallery
- Providing fallback function and allow override by plugin
- No compression occurs on my jpegs after adding jpeg_quality hook to my functions.php file
- Update add_image_size
- update_post_meta not working in function
- Function the_content
- Set thumbnail from URL, by grabbing image in functions.php
- Dynamically adding Captions to images
- Full size image option removing problem
- Function to get thumbnail img source
- does acf_save_post cause endless loop?
- Call ACF data from functions.php [closed]
- Image width issue in IE [closed]
- image_size with respect to aspect ratios
- How to resize image from import image url?