Created
March 8, 2012 01:18
-
-
Save bdgeorge/1997786 to your computer and use it in GitHub Desktop.
The _fix_attachment_links function from wordpress 2.8 - fixes attachment link problems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function _fix_attachment_links( $post_ID ) { | |
| $post = & get_post( $post_ID, ARRAY_A ); | |
| $search = "#<a[^>]+rel=('|\")[^'\"]*attachment[^>]*>#ie"; | |
| // See if we have any rel="attachment" links | |
| if ( 0 == preg_match_all( $search, $post['post_content'], $anchor_matches, PREG_PATTERN_ORDER ) ) | |
| return; | |
| $i = 0; | |
| $search = "#[\s]+rel=(\"|')(.*?)wp-att-(\d+)\\1#i"; | |
| foreach ( $anchor_matches[0] as $anchor ) { | |
| if ( 0 == preg_match( $search, $anchor, $id_matches ) ) | |
| continue; | |
| $id = (int) $id_matches[3]; | |
| // While we have the attachment ID, let's adopt any orphans. | |
| $attachment = & get_post( $id, ARRAY_A ); | |
| if ( ! empty( $attachment) && ! is_object( get_post( $attachment['post_parent'] ) ) ) { | |
| $attachment['post_parent'] = $post_ID; | |
| // Escape data pulled from DB. | |
| $attachment = add_magic_quotes( $attachment); | |
| wp_update_post( $attachment); | |
| } | |
| $post_search[$i] = $anchor; | |
| $post_replace[$i] = preg_replace( "#href=(\"|')[^'\"]*\\1#e", "stripslashes( 'href=\\1' ).get_attachment_link( $id ).stripslashes( '\\1' )", $anchor ); | |
| ++$i; | |
| } | |
| $post['post_content'] = str_replace( $post_search, $post_replace, $post['post_content'] ); | |
| // Escape data pulled from DB. | |
| $post = add_magic_quotes( $post); | |
| return wp_update_post( $post); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or perhaps it doesn't...
The preg_replace still kills it with get_attachment_link