Last active
December 13, 2017 18:15
-
-
Save cfxd/219f083465bb5b93fac8 to your computer and use it in GitHub Desktop.
Remove Existing Self Linking Images ("WP Selfies") in WordPress. See http://cfxdesign.com/remove-existing-self-linking-images-in-wordpress
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 remove_self_linking_images() { | |
$all_ids = new WP_Query(array( | |
'post_type' => array('post', 'page'), // feel free to add custom post types here if necessary | |
'posts_per_page' => -1, | |
'post_status' => 'any', | |
'fields' => 'ids' | |
)); | |
foreach($all_ids->posts as $id) { | |
$current_post = get_post($id); | |
$current_content = $current_post->post_content; | |
$content_replaced = $current_content; | |
preg_match_all('/<a.*?href=\"(.*?)\".*?>(<img.*?src=\"(.*?)\".*?>)<\/a>/', $current_content, $matches); | |
$wp_selfies = $matches[0]; | |
if(count($wp_selfies) > 0) { | |
$wp_selfies_urls = $matches[1]; | |
$wp_selfies_imgs = $matches[2]; | |
$wp_selfies_srcs = $matches[3]; | |
foreach($wp_selfies as $key => $selfie) { | |
if($wp_selfies_urls[$key] === $wp_selfies_srcs[$key]) { | |
$content_replaced = str_replace($selfie, $wp_selfies_imgs[$key], $content_replaced); | |
} | |
} | |
wp_update_post(array( | |
'ID' => $id, | |
'post_content' => $content_replaced | |
)); | |
} | |
} | |
} | |
//add_action('admin_init', 'remove_self_linking_images'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment