wppaste
WordPress

_fix_attachment_links( int|WP_Post $post ): void|int|WP_Error

Since
2.3.0
Source
wp-admin/includes/post.php:1151
Replaces hrefs of attachment anchors with up-to-date permalinks.

Compatibility

WordPress
since 2.3.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$postint|WP_Post
Post ID or post object.

Return value

void|int|WP_Error
Void if nothing fixed. 0 or WP_Error on update failure. The post ID on update success.

Performance profile

How much work a call to _fix_attachment_links() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
13–47

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 98.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • optionoption read or writeget_option()called directly
  • regexregular expression over the whole inputpreg_match_all()called directly
  • hookthird-party callbacksapply_filters()one call below _fix_attachment_links()
  • cacheobject cachewp_cache_get()one call below _fix_attachment_links()
  • serializeserialisationmaybe_unserialize()one call below _fix_attachment_links()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost _fix_attachment_links() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always13–18get_post(), get_option()
get_option() && $content && !preg_match_all()24get_post(), get_option(), preg_match_all()
get_option() && $content && preg_match_all()37–38get_post(), get_option(), preg_match_all(), get_bloginfo()
get_option() && $content && preg_match_all()46–47get_post(), get_option(), preg_match_all(), get_bloginfo(), add_magic_quotes(), wp_update_post()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9813–4715
8.59813–4715
8.49813–471524 fewer instructions than PHP 8.3
8.312213–5615
8.212213–5615
8.112213–56151 fewer instruction than PHP 7.4
7.412313–5615

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 7

Used by · 2

  • edit_post()Updates an existing post with values provided in `$_POST`.
  • wp_write_post()Creates a new post from the "Write Post" form using `$_POST` information.

Source code

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 );	}}

Changelog

Introduced in 2.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-admin/includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.