Created
September 15, 2015 17:31
-
-
Save josephbergdoll/5cc461431d4a2790ccb6 to your computer and use it in GitHub Desktop.
This file contains 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
// Disable <a> tags around images by default | |
function no_imagelinks_content() { | |
$image_set = get_option( 'image_default_link_type' ); | |
if ($image_set !== 'none') { | |
update_option('image_default_link_type', 'none'); | |
} | |
} | |
add_action('admin_init', 'no_imagelinks_content', 10); | |
// Remove them from existing posts | |
function attachment_image_link_remove_filter() { | |
ob_start(); | |
$postArgs = array( | |
'post_type' => 'post', | |
'posts_per_page' => -1, | |
'suppress_filters' => true, | |
'post_status' => 'any', | |
'orderby' => 'date', | |
'order' => 'ASC', | |
); | |
$posts = get_posts($postArgs); | |
foreach ($posts as $post) { | |
$postid = $post->ID; | |
$postContent = $post->post_content; | |
$postModified = $post->post_modified; | |
$postModifiedGMT = $post->post_modified_gmt; | |
// Remove links | |
$postContent = | |
preg_replace( | |
array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}','{ wp-image-[0-9]*" /></a>}'),array('<img','" />'), | |
$postContent); | |
// Update Post | |
$my_post = array( | |
'ID' => $postid, | |
'post_content' => $postContent, | |
'post_modified' => $postModified, | |
'post_modified_gmt' => $postModifiedGMT | |
); | |
$result = wp_update_post($my_post); | |
if ($result > 0) { | |
log_it('Success! Updated Post ID ' . $result); | |
} | |
else { | |
log_it('Failure — Post #'.$postid); | |
} | |
} | |
ob_clean(); | |
} | |
// add_action('wp_head', 'attachment_image_link_remove_filter'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment