Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sabrina-zeidan/ce2161dcc9917f7f097e2813c2ec814e to your computer and use it in GitHub Desktop.
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]
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);
}
}
@juniorbra
Copy link

Hello, this function did not work. I put it in functions.php. Is there any other way to use it?

@sabrina-zeidan
Copy link
Author

@juniorbra which action hook did you use?

@juniorbra
Copy link

@sabrina-zeidan I just copied your function and pasted it into functions.php.

@sabrina-zeidan
Copy link
Author

@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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment