How to update an image attachment’s alt text from a custom field when saving a post?

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 … Read more

WordPress Attachment Page URL Rewrite!

There are three issues in your code: In your custom wp_attachment_link() function, you should use the post slug ($attachment->post_name) instead of simply replacing the (whitespaces) with – (dash) in the post title — and note that the resulting slug could be different than the actual slug, e.g. the title could be My Image with the … Read more

Shortcode for Custom Field of Media Attachment (to use with Featured Images)

The most basic option I can think of is this add_shortcode(‘bt_featured_image_acf_location’, ‘bt_featured_image_acf_location’); function bt_featured_image_acf_location ($atts) { return get_field(‘location’, get_post_thumbnail_id()); } Change ‘location’ to your acf field name and thats it. get_post_thumbnail_id() will get the current post featured image id. Once you have the ID of the post (images are posts, they are of the post … Read more

Get all post embedded images

You can use DOMDocument to get every image from any page: function testingdom(){ $dom = new DOMDocument(); libxml_use_internal_errors(true); $dom->loadHTMLFile(‘https://the_post_url.com/anyone’); $data = $dom->getElementsByTagName(“img”); $srcs = array(); foreach($data as $key => $dat){ $srcs[] = $data->item($key)->getAttribute(“src”); } $dom = null; } add_action(“wp_head”, “testingdom”); This way you should have every src in an array called srcs