Created
July 29, 2017 09:25
-
-
Save sabrina-zeidan/ce2161dcc9917f7f097e2813c2ec814e to your computer and use it in GitHub Desktop.
This function will delete links framing images in your post content permanently [Wordpress]
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
function remove_image_link_permanently(){ | |
$all_posts = get_posts( array( | |
'post_type' => 'post', | |
'numberposts' => -1, | |
'status' => 'any' | |
)); | |
foreach ($all_posts as $single_post){ | |
$post_id = $single_post->ID; | |
$content = $single_post->post_content; | |
$updated_content = | |
preg_replace( | |
array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}', | |
'{ wp-image-[0-9]*" /></a>}'), | |
array('<img','" />'), | |
$content | |
); | |
$updated_post = array( | |
'ID' => $post_id, | |
'post_content' => $updated_content, | |
); | |
$update_post = wp_update_post($updated_post); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@juniorbra this is a function. You need to connect it to the action to fire it. For example, after_setup_theme action, then to fire it you'll use:
add_action('after_setup_theme', 'remove_image_link_permanently');
+ would be nice to check the actioned you hook to, runs only once.I would recommend getting acquainted with WP Actions and Hooks concept before all this. Or make a backup at least :)