1

Is it possible either through PHP or SQL to update Wordpress image URLs without using search and replace?

I ask because I have several hundred that I need to get changed but I can't use search and replace because each URL has a unique number that I need to remove.

It's the number right after the month in the Wordpress image URL. Each one is unique and I need to have it removed to have the image display correctly.

What the URL is currently: https://example.com/wp-content/uploads/2025/02/14212828/shutterstock_2345459.jpg

What the URL should be: https://example.com/wp-content/uploads/2025/02/shutterstock_2345459.jpg

2 Answers 2

2

If your DBMS (which you did not specify using a tag) supports regular expressions, and, with that, the REGEXP_REPLACE() function, use it to replace a pattern consisting of a forward slash, five or more digits and again a forward slash, with just a forward slash:

WITH
-- your input ... plus another row with a different number as directory
indata(url) AS (
            SELECT 'https://example.com/wp-content/uploads/2025/02/14212828/shutterstock_2345459.jpg'
  UNION ALL SELECT 'https://example.com/wp-content/uploads/2025/02/28224848/shutterstock_2024121.jpg'
)
--- real query starts here ..
SELECT 
  REGEXP_REPLACE(url,'/\d{5,}/','/') AS new_url
, url
FROM indata;
|new_url                                                                |url                                                                             |
|-----------------------------------------------------------------------|--------------------------------------------------------------------------------|
|https://example.com/wp-content/uploads/2025/02/shutterstock_2345459.jpg|https://example.com/wp-content/uploads/2025/02/14212828/shutterstock_2345459.jpg|
|https://example.com/wp-content/uploads/2025/02/shutterstock_2024121.jpg|https://example.com/wp-content/uploads/2025/02/28224848/shutterstock_2024121.jpg|
4
  • 1
    perhaps more than just one or more, as this would replace the year which comes early, doesn't it? thinking in the direction of /\d\d\d\d\d+/ (or square brackets if the regex engine supports it: /\d{5,}/).
    – hakre
    Commented 22 hours ago
  • 1
    I like the idea of using REGEXP_REPLACE. Thanks for that. As @hakre pointed out, I think I just need to play with the regexp pattern to make it work correctly.
    – Stepppo
    Commented 20 hours ago
  • 1
    @Stepppo: If you already like the idea, upvote the answer. If you have even approved it working, accept it so it gets the green checkmark. btw. I also think this is a good approach IIUC.
    – hakre
    Commented 18 hours ago
  • Indeed you're right @hakre! And usually, DBMS-s supporting the Regexp functions abide to the perl standard. So it's indeed \d{5,}. I'll fix the answer accordingly - and so that the result is immediately visible, too Commented 12 hours ago
0

The WordPress filter wp_get_attachment_url can be used to modify your attachment URL on demand without the need to modify your database contents.

add_filter('wp_get_attachment_url', 'modify_the_url');

function modify_the_url($url) {
    // Do something with your URL
    return $url;
}

Like most hooks/filters, there's additional parameters available to you if you set the accepted accepted_args depending on context.

add_filter('wp_get_attachment_url', 'modify_the_url', accepted_args: 2);

Allow for the attachment post ID to be available to your function

function modify_the_url($url,$post_id)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.