WordPress filter hook · part of WordPress core

wp_get_attachment_url

A WordPress core filter that 11 plugins also fire themselves, running on 14M+ sites between them.

“Filter the attachment URL.”

How to use wp_get_attachment_url

It's a filter: your callback receives the value and 1 more argument, and must return the (changed) value. Add this to your theme's functions.php or a small custom plugin:

functions.php
add_filter( 'wp_get_attachment_url', function( $src, $post_id ) {
	// Change $src here.
	return $src;
}, 10, 2 );

Argument names come from Yoast SEO's source. Other plugins may pass different values: check the call in the plugin you're targeting below.

This hook is fired by WordPress core too. Official reference on developer.wordpress.org →

11 plugins that fire wp_get_attachment_url

Sorted by active installs. Each entry shows where the plugin calls the hook and what it passes.

  1. Yoast SEO icon Yoast SEO10+ million installs apply_filters( 'wp_get_attachment_url', $src, $post_id ) inc/sitemaps/class-sitemap-image-parser.php:407
  2. Rank Math SEO icon Rank Math SEO4+ million installs apply_filters( 'wp_get_attachment_url', $src, $post_id ) includes/modules/sitemap/class-image-parser.php:406
  3. SOOZ icon SOOZ2,000+ installs apply_filters( 'wp_get_attachment_url', $canonical_url, $post_id ) includes/core/attachments-media.php:1244
  4. MaxGalleria icon MaxGalleria1,000+ installs apply_filters( 'wp_get_attachment_url', $url, $attachment->ID ) maxgalleria.php:1139
  5. ARI Stream Quiz icon ARI Stream Quiz1,000+ installs apply_filters( 'wp_get_attachment_url', $url, $post->post_id ) libraries/arisoft/core/wordpress/class-helper.php:135
  6. Images via imgix icon Images via imgix70+ installs apply_filters( 'wp_get_attachment_url', $image_src ) includes/class-images-via-imgix.php:190
  7. LTI Sitemap icon LTI Sitemap10+ installs apply_filters( 'wp_get_attachment_url', $url, $post->ID ) src/generators/sitemap.php:333
  8. Post ID Optimizer icon Post ID Optimizer10+ installs apply_filters( 'wp_get_attachment_url', $url, $post->ID ) post-poidopt.php:5654
  9. CloudSync Master icon CloudSync Master10+ installs apply_filters( 'wp_get_attachment_url', $localUrl, 0 ) includes/WP/CloudSyncMaster/Admin/Table/ColumnTypeBuilder/ObjectLinkBuilder.php:140
  10. Rooster LMS icon Rooster LMSFewer than 10 installs apply_filters( 'wp_get_attachment_url', $full, $id ) includes/Events/EventPosts.php:135
  11. AgenticCart icon AgenticCartFewer than 10 installs apply_filters( 'wp_get_attachment_url', $attachment_url, (int) $att->ID ) classes/sync/class-agentic-bulk-processor.php:575

Need a customisation built on this hook?

Tell us what you want to change and which plugin it's for. We'll write it, test it and send it back as a small plugin.