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 attachment_link_convert($content){ | |
| global $wpdb; | |
| $upload_dir = wp_upload_dir(); | |
| $pattern = '#(<a [^>]*href=[\'"])('.preg_quote($upload_dir['baseurl']).'/)([^\'"]*)([\'"][^>]*><img [^>]*></a>)#uism'; | |
| if ( preg_match_all($pattern, $content, $matches, PREG_SET_ORDER) ) { | |
| foreach ( $matches as $match ) { | |
| $attachment_id = $wpdb->get_var($wpdb->prepare(" | |
| select p.ID | |
| from {$wpdb->posts} as p | |
| inner join {$wpdb->postmeta} as m on p.ID = m.post_id | |
| where m.meta_key = %s | |
| and m.meta_value = %s | |
| limit 1", | |
| '_wp_attached_file', | |
| $match[3])); | |
| if ( $attachment_id ) { | |
| $attachment_url = get_attachment_link($attachment_id); | |
| $new_link = $match[1].$attachment_url.$match[4]; | |
| $content = str_replace($match[0], $new_link, $content); | |
| } | |
| } | |
| } | |
| unset ($matches); | |
| return $content; | |
| } | |
| add_filter('the_content','attachment_link_convert'); |
WordPressで画像のリンク先を強制的に添付ファイル投稿URLに変更する方法がやばすぎるwwww への返信 コメントをキャンセル