Title: _fix_attachment_links
Published: April 25, 2014
Last modified: February 24, 2026

---

# _fix_attachment_links( int|WP_Post $post ): void|int|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/_fix_attachment_links/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/_fix_attachment_links/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/_fix_attachment_links/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/_fix_attachment_links/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_fix_attachment_links/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/_fix_attachment_links/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Replaces hrefs of attachment anchors with up-to-date permalinks.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/_fix_attachment_links/?output_format=md#parameters)󠁿

 `$post`int|[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)
required

Post ID or post object.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/_fix_attachment_links/?output_format=md#return)󠁿

 void|int|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
Void if nothing fixed. 0 or [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
on update failure. The post ID on update success.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/_fix_attachment_links/?output_format=md#source)󠁿

    ```php
    function _fix_attachment_links( $post ) {
    	$post    = get_post( $post, ARRAY_A );
    	$content = $post['post_content'];

    	// Don't run if no pretty permalinks or post is not published, scheduled, or privately published.
    	if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ), true ) ) {
    		return;
    	}

    	// Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero).
    	if ( ! strpos( $content, '?attachment_id=' ) || ! preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) ) {
    		return;
    	}

    	$site_url = get_bloginfo( 'url' );
    	$site_url = substr( $site_url, (int) strpos( $site_url, '://' ) ); // Remove the http(s).
    	$replace  = '';

    	foreach ( $link_matches[1] as $key => $value ) {
    		if ( ! strpos( $value, '?attachment_id=' ) || ! strpos( $value, 'wp-att-' )
    			|| ! preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match )
    			|| ! preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) ) {
    				continue;
    		}

    		$quote  = $url_match[1]; // The quote (single or double).
    		$url_id = (int) $url_match[2];
    		$rel_id = (int) $rel_match[1];

    		if ( ! $url_id || ! $rel_id || $url_id !== $rel_id || ! str_contains( $url_match[0], $site_url ) ) {
    			continue;
    		}

    		$link    = $link_matches[0][ $key ];
    		$replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );

    		$content = str_replace( $link, $replace, $content );
    	}

    	if ( $replace ) {
    		$post['post_content'] = $content;
    		// Escape data pulled from DB.
    		$post = add_magic_quotes( $post );

    		return wp_update_post( $post );
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/post.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/post.php#L1158)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/post.php#L1158-L1204)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_fix_attachment_links/?output_format=md#related)󠁿

| Uses | Description | 
| [add_magic_quotes()](https://developer.wordpress.org/reference/functions/add_magic_quotes/)`wp-includes/functions.php` |

Walks the array while sanitizing the contents.

  | 
| [get_attachment_link()](https://developer.wordpress.org/reference/functions/get_attachment_link/)`wp-includes/link-template.php` |

Retrieves the permalink for an attachment.

  | 
| [wp_update_post()](https://developer.wordpress.org/reference/functions/wp_update_post/)`wp-includes/post.php` |

Updates a post with new post data.

  | 
| [get_bloginfo()](https://developer.wordpress.org/reference/functions/get_bloginfo/)`wp-includes/general-template.php` |

Retrieves information about the current site.

  | 
| [get_option()](https://developer.wordpress.org/reference/functions/get_option/)`wp-includes/option.php` |

Retrieves an option value based on an option name.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/_fix_attachment_links/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/_fix_attachment_links/?output_format=md#)

| Used by | Description | 
| [wp_write_post()](https://developer.wordpress.org/reference/functions/wp_write_post/)`wp-admin/includes/post.php` |

Creates a new post from the “Write Post” form using `$_POST` information.

  | 
| [edit_post()](https://developer.wordpress.org/reference/functions/edit_post/)`wp-admin/includes/post.php` |

Updates an existing post with values provided in `$_POST`.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/_fix_attachment_links/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.3.0](https://developer.wordpress.org/reference/since/2.3.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_fix_attachment_links%2F)
before being able to contribute a note or feedback.