Absolute to Relative path in Wordpress Gutenberg

Read time: 3 minutes

User Profile

Hamad K

Jan 22, Mon

In Wordpress Gutenberg editor, when we set a path, it automatically fetches the base_url and render it as is! however, we usually require relative path:


To achieve that, simply add the following code in your theme (function.php)


add_filter( 'content_save_pre', function ( $content ) {
$base_url = home_url(); // Get the base URL of your WordPress site
$current_url = get_permalink(); // Get the current URL of the post or page

// Replace absolute links with relative links based on the current domain
$content = str_replace( $base_url, '', $content );
$content = str_replace( $current_url, '', $content );

return$content;
} );


Save the functions.php file, and see the magic happen. Now, your web addresses will be simpler and easier to work with in WordPress.


Remember, like any magic, use this wisely to keep your website in balance. Enjoy coding!


You may also like

err_too_many_redirects

Interested in Publishing your knowledge and sharing it with the world?

or ask anything about our services

We thrive by partering with visionary brands and driven individuals.

Say hello 👋

[email protected]