How to completely remove active image links

Hi,

How can I completely remove active image links from current images? We have imported posts from blogger.com to my clients WordPress website. Unfortunately, the images still have their link to the the blogger server where the images still are uploaded.

So I want to completely remove all current images links. I believe I need a smart php function to do this. Maybe a preg_replace function or alike :slight_smile:

I have already found this article https://premium.wpmudev.org/blog/how-to-remove-links-from-wordpress-images/ and checked out the stock-exchange link. However, I am not looking for a filter but for a function that does a one time job :smiley:

Sincerely,

Mika

  • Ash
    • WordPress Hacker

    Hello @mika

    I hope you are well today and thanks for asking the question.

    The function shared in the blog article, is the standard procedure.

    add_filter( 'the_content', 'attachment_image_link_remove_filter' );
    function attachment_image_link_remove_filter( $content ) {
    $content = preg_replace( array('{<a(.*?)(wp-att|wp-content/uploads)[^>]*><img}', '{ wp-image-[0-9]*" /></a>}'), array('<img','" />'), $content );
    return $content;
    }

    Would you please let me know what do you mean by “one time job”?

    Cheers

    Ash

  • Mika
    • The Bug Hunter

    Hey Ash

    Good to hear fro you and thanks for your reply!

    This code seems to do the job, however, I want it to only occur once with the current posts so that all image links from the current posts are removed.

    One time job: Currently the website has images with links. I want all those image links removed. Then in the future, I want my client to have the option to add links or not.

    The main issue at hand is that many of the current links is pointing back to my clients blogger.com server. We do not want any imported images to have links.

    Hope that make sense :slight_smile:

    Sincerely,

    Mika

  • Vinod Dalvi
    • WP Unicorn

    Hi Mika,

    Thank you for your reply.

    You can just temporary add the following code in the functions.php file of your theme that will remove all image links from the post content on your site.

    $args = array( 'posts_per_page' => -1 );

    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post );
    $content =
    preg_replace(
    array('{<a(.*?)(wp-att|wp-content/uploads)[^>]*><img}',
    '{ wp-image-[0-9]*" /></a>}'),
    array('<img','" />'),
    $post->post_content
    );

    $my_post = array(
    'ID' => $post->ID,
    'post_content' => $content,
    );

    // Update the post into the database
    wp_update_post( $my_post );
    endforeach;
    wp_reset_postdata();

    Please note the above code will remove the anchor tag from around the image tag.

    If you don;t want to do this for all posts but for specific posts or for posts having specific category then you can pass appropriate parameter to the get_posts function as described on the following page.

    https://codex.wordpress.org/Class_Reference/WP_Query

    If you don’t want to add the provided code in your theme file then you can just use the attached small plugin containing the above code.

    After using the above code when you refresh your site then all posts content will get updated removing image links so in this process your site will be a bit slow so don’t refresh the page. After finishing this process you can just remove the custom code from your site so that it won’t execute again.

    If you have many posts and your site displays timeout error in this process then please update the posts in batch using offset parameter of get_posts function.

    I highly recommend you to take backup of your site using the following snapshot plugin before using the above code so that if anything goes wrong then we can recover it.

    https://premium.wpmudev.org/project/snapshot/

    Best Regards,

    Vinod Dalvi

  • Mika
    • The Bug Hunter

    Hey Vinod.

    Amazing, just what I needed.

    Now, I can see that the preg_replace() function target images uploaded to the WP uploads folder, however, how can I change it so that it target all images starting with url “http://3.bp.blogspot.com/”

    Once again, thanks a lot!!

    Sincerely,

    Mika

  • Vinod Dalvi
    • WP Unicorn

    Hi Mika,

    Glad i could help you.

    To remove the anchor tags having specific URLs you can try changing the following code snippet in my shared above code.

    Before Editing :

    $content =
    preg_replace(
    array('{<a(.*?)(wp-att|wp-content/uploads)[^>]*><img}',
    '{ wp-image-[0-9]*" /></a>}'),
    array('<img','" />'),
    $post->post_content
    );

    After Editing :

    $content =
    preg_replace(
    array('{<a(.*?)(http://3.bp.blogspot.com)[^>]*><img}',
    '{ wp-image-[0-9]*" /></a>}'),
    array('<img','" />'),
    $post->post_content
    );

    You can also try creating this regex yourself using the following tool.

    https://regex101.com/

    Regards,

    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.