• 25/04/2019 • 3 phút đọc • 10 lượt xem

How to Insert Relative Image URLs in WordPress

Việt Lâm Coder
Việt Lâm Coder
Senior Fullstack & System Architect • Hotline: 0944.71.4545

There are certain issues that polarize web developers. Choice of technology, interpreted versus compiled code, Android versus iPhone etc. One of these relates to WordPress and whether or not we should use absolute or relative URLs. Absolute URLs include the entire URL as you see it in the address bar of your browser including the root domain name. Relative URLs on the other hand specify internal URLs only with regard to the current domain – in other words, they leave out the domain name entirely when referring to URLs belonging to the same site.

This has several advantages. Notably, it allows you to seamlessly move your WordPress installation between different domains without 404 not found errors. Also, relative URLs ease the transition between development and production environments. If you use absolute URLs, you’re going to have to perform a database search and replace them each and every time you push test code onto a live server.

This doesn’t mean that relative URLs are always superior to absolute once. Many prominent coders have raised concerns regarding relative URLs and spidering, duplicated content etc. But if these concerns are not enough to hold you back, this article will show you how to automatically insert relative URLs for WordPress images via the visual editor.

WordPress and Absolute URLs


Let’s take an example and look at the default behavior of WordPress. When you want to insert an image into the visual editor, you click the “Add Media” button where you can select an image as well as the size and insert it into your post as shown here:

inserting image

After the image has been posted onto the visual editor, you can view the source code by clicking the “Text” tab on the right-hand side. Examining the output code, you can see that WordPress includes the full absolute URL for the image source, including the domain name and the protocol.

absolute URL inserted

You can easily imagine what will happen if you migrate your blog to another website with a different domain name. These links won’t go anywhere and all the images on your site will break unless you do some specialized redirects or replace all mentions of the old domain name directly in your database. Now let’s see how to change this behavior.

Changing to Relative URLs


In order to change this behavior, we’re going to add a filter to replace the absolute URL in the HTML with a relative one. Open up your functions.php file and paste the following code before the closing ?> PHP tag:

function switch_to_relative_url($html, $id, $caption, $title, $align, $url, $size, $alt)
{
$imageurl = wp_get_attachment_image_src($id, $size);
$relativeurl = wp_make_link_relative($imageurl[0]);
$html = str_replace($imageurl[0],$relativeurl,$html);

return $html;
}
add_filter('image_send_to_editor','switch_to_relative_url',10,8);






If you don’t know how to add custom code to WordPress, here’s my earlier tutorial on how to do this.

The code above hooks into the ‘image_send_to_editor” filter which converts the absolute URL using the internal function “wp_make_link_relative” and then simply replaces the original one with it. After saving your changes in functions.php, you can see that WordPress now inserts relative URLs for images into the post editor instead of absolute ones:

images using relative URL

Now when you move your WordPress site from your local development environment to the production server, you won’t be required to make any changes to the database via a find and replace function. You can also safely transfer your websites to another domain entirely and your images will still display correctly. That’s the magic of relative URLs.

Keep in mind of course that this won’t make any modifications to images that you’ve already uploaded and inserted into previous posts. You’ll need to replace them manually instead.

Việt Lâm Coder

Việt Lâm Coder

Tác giả bài viết và chuyên gia quản trị website. Nếu bạn gặp khó khăn trong quá trình thực hiện hoặc có nhu cầu thiết kế website, lập trình web app Laravel, hãy liên hệ ngay với tôi qua Hotline / Zalo: 0944.71.4545.

☕ Mời Tác Giả Ly Cà Phê Ấm Lòng

Nếu bài viết này giúp ích cho công việc của bạn, hãy tiếp thêm động lực cho tác giả duy trì kho tri thức bằng một ly cà phê ấm lòng nhé!

Mã QR ủng hộ tác giả
Ngân hàng TMCP Quân Đội (MB Bank)TONG VIET LAM

Có Thể Bạn Quan Tâm

Cách Mở Tab Ẩn Danh Trên Safari MacBook Đơn Giản

Cách mở tab ẩn danh trên Safari MacBook sẽ giúp cho người dùng có thể bảo vệ quyền riêng tư và duyệt web một cách an toà...

Hướng dẫn 5 cách tắt update Windows 11 vĩnh viễn chi tiết nhất

Mặc định Windows 11 sẽ được tự động cập nhật trong nền. Tuy nhiên nếu vì lý do nào đó mà bạn không muốn nhận các bản...

Hướng dẫn trỏ file hosts cho domain trên hệ điều hành Windows

– Làm cách nào để 1 website có thể hoạt động trên Share hosting, VPS, Server mà không cần cấu hình các record DNS cho do...

Kích hoạt Cloudflare Turnstile trong Contact Form 7 chống spam

Cloudflare Turnstile là giải pháp xác thực không dùng CAPTCHA giúp bảo vệ website WordPress khỏi spam một cách nhẹ nhàng...

Cách Sử Dụng DeepSeek R1 Miễn Phí Trong Visual Studio Code Với Cline?

Hãy đối mặt với thực tế: Các trợ lý lập trình AI như GitHub Copilot thì tuyệt vời, nhưng chi phí đăng ký của chúng có th...

Gmail API – Tất cả những gì bạn cần biết - 2025

Nếu bạn đang tìm cách tích hợp Gmail vào các ứng dụng của riêng mình thì Gmail API chính là thứ bạn cần! Gmail là một lự...

Hướng dẫn tạo màn hình desktop ảo bằng Task View trên Surface Windows 11

Việt Lâm Coder sẽ hướng dẫn tạo lập nhiều màn hình desktop ảo bằng Task View trên Surface Windows 11 để sắp xếp các tác...

How to Transfer PuTTY Sessions To Another Windows Machine

his is an user contributed article. PuTTY is a terminal emulator application which can act as a client for the SSH, T...

Hướng dẫn cách kiểm tra cổng đang mở (Check open port) trên linux

Với nhiều năm kinh nghiệm hỗ trợ Khách hàng của Tenten, chúng tôi nhận thấy việc thường xuyên theo dõi kiểm tra hoạt độn...

Kiểm tra tất cả các port mà Cyber Panel đang cần có được mở hay không ?

Để kiểm tra tất cả các port cùng một lúc, bạn có thể sử dụng lệnh: netstat -tulpn | grep -E ':(8090|80|443|21|40110|401...

Hướng dẫn cài đặt KeyHelp Panel

Tóm tắt về KeyHelp Bạn đã có phần cứng tốt nhất cho các dự án Internet của mình nhưng việc quản lý phức tạp, hiệu suất...

Tổng hợp danh sách Control Panel VPS free tốt nhất hiện nay

Control Panel VPS free cung cấp một lựa chọn tuyệt vời cho những người dùng muốn quản lý VPS một cách dễ dàng và hiệ...