Categories
URL / Rewrite Filter

How to get WordPress REST API to use SITE URL instead of HOME URL in Guttenberg editor

If you have a different SITE URL and HOME URL ( WordPress Address ), then, WordPress will use HOME URL for WordPress API. Here is an example: If your SITE URL is mywebsite.com but your HOME URL or WordPress Address ( URL ) is mywebsite.com/blog/, Then, your WordPress API URL will be mywebsite.com/blog/wp-json/wp/v2/. But you may want to change it to mywebsite.com/wp-json/wp/v2/.

Here is the snippet code is given below, which uses ‘rest_url‘ filter to replace the HOME URL in REST API URL to SITE URL.

// change WordPress API URL to HOME URL
add_filter('rest_url', 'wptips_home_url_as_api_url');
function wptips_home_url_as_api_url($url) {
    $url = str_replace(home_url(),site_url() , $url);
    return $url;
}

By Murali Kumar

I am a Full Stack Web Developer with over 12 years of experience and I love WordPress & Woocommerce.

If you need help of a WordPress developer, Get in touch now!.

Leave a Reply

Your email address will not be published. Required fields are marked *