_fix_attachment_links( int|WP_Post $post ): void|int|WP_Error
- Since
- 2.3.0
- Source
wp-admin/includes/post.php:1145
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
- Scaling
- Scales with input
- Instructions
- 13–47
- Plugin surface
- None
- Called by
- 2
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 98.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - optionoption read or write
get_option()called directly - regexregular expression over the whole input
preg_match_all()called directly - hookthird-party callbacks
apply_filters()one call below _fix_attachment_links() - cacheobject cache
wp_cache_get()one call below _fix_attachment_links() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 13–18 | get_post(), get_option() |
get_option() && $content && !preg_match_all() | 24 | get_post(), get_option(), preg_match_all() |
get_option() && $content && preg_match_all() | 37–38 | get_post(), get_option(), preg_match_all(), get_bloginfo() |
get_option() && $content && preg_match_all() | 46–47 | get_post(), get_option(), preg_match_all(), get_bloginfo(), add_magic_quotes(), wp_update_post() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 98 | 13–47 | 15 | |
| 8.5 | 98 | 13–47 | 15 | |
| 8.4 | 98 | 13–47 | 15 | 24 fewer instructions than PHP 8.3 |
| 8.3 | 122 | 13–56 | 15 | |
| 8.2 | 122 | 13–56 | 15 | |
| 8.1 | 122 | 13–56 | 15 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 123 | 13–56 | 15 |
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
- get_post()Retrieves post data given a post ID or post object.
- get_option()Retrieves an option value based on an option name.
- get_bloginfo()Retrieves information about the current site.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- get_attachment_link()Retrieves the permalink for an attachment.
- add_magic_quotes()Walks the array while sanitizing the contents.
- wp_update_post()Updates a post with new post data.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.