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
- 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
- Programmatically Set First Image as Featured
- WP 4.4. responsive loads normal image after loading the responsive image
- How to reduce original image quality on upload?
- Show/hide Widgets in Dashboard Based on Current Advanced Custom Fields Option
- Set image size on media and text block with a function
- Create thumbnail on PDF upload with Gravity Forms
- Remove images from get_the_excerpt
- Use Exact Image Size using add_image_size
- Insert width & height attributes to all images displayed on each page
- Converting a simple plugin to be placed inside of functions.php
- 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
- How to add css class to image attached in all the posts?
- Add custom image size not working
- Count total number of images in post and echo results as number
- how to add an image to the footer?
- How do I permanently Disable Attachment Post URL
- 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
- Understanding and altering the structure of posted images
- 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?
- Can’t filter wp_get_attachment_link
- Get ID of first image attached to a post
- How to stop wordpress from injecting hard-coded style into image attachments with captions
- Control the srcset much more (not all sizes in it each time)
- Way to store featured image width and height in two separate variables?
- srcset and custom field get_post_meta and wp_get_attachment_url
- Visual Composer creating own shortcodes with vc_map() to return simple Image
- Add php code to wp_print_scripts?
- How to get an attachment id from a filename
- Access category within rss2_head hook?
- 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 convert image url to a Base_64 data url with wordpress function..
- Function to insert missing image size attributes into img tags
- Get Attachment Category Name
- Image Crop Not Working
- Use existing image sizes for WooCommerce
- Insert a field with PREG_REPLACE – strange behaviour
- Retrieve First Image Function
- Using add_image_size in functions.php to get original uploaded file
- How d0 i get the number of attachments in the post
- Display ACF category image on archive and single template files
- Custom image sizes showing in Classic Editor only when upladed directly to post
- Override a Post’s URL with Advanced Custom Fields Function
- WordPress Function Assistance with Loop
- 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
- 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?
- Images with overlay
- Reference multiple style sheets, clearing styles for permalink page, custom fields for css
- get_field() with ID in one signal filter not working
- Echo a String Based On Geolocation? [Woocommerce || ACF]
- Comment function for page
- Printing medium sized image
- restrict uploaded image size and fixed image display size
- 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)
- Zip all (original) images from media gallery
- Set thumbnail from URL, by grabbing image in functions.php
- Function to get thumbnail img source
- does acf_save_post cause endless loop?
- 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