Skip to content

Instantly share code, notes, and snippets.

@lightningspirit
Last active August 22, 2022 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lightningspirit/61c8810778a3a88192b3ca5060dc3efb to your computer and use it in GitHub Desktop.
Save lightningspirit/61c8810778a3a88192b3ca5060dc3efb to your computer and use it in GitHub Desktop.
Fix WordPress REST base to use real URL

WordPress REST base on Site URL

If you change the site URL option to use a different one from your installed directory, the /wp-json/ is base changes too. This plugin fixes that.

Usage

  1. Download a ZIP of this plugin
  2. Upload to WordPress
<?php
/**
* Plugin Name: Fix REST URL on Site URL
* Plugin URI: https://gist.github.com/lightningspirit/61c8810778a3a88192b3ca5060dc3efb
* Description: Fix WordPress REST base to use real URL
* Author: Move Your Digital, Inc.
* Author URI: https://moveyourdigital.com
* Version: 0.1.0
*
* @package Fix_Rest_Url_Site_Url
*/
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
add_filter('home_url', function ($url, $path, $origin_scheme, $blog_id) {
if ($origin_scheme == "rest") {
return get_site_url($blog_id, $path);
} elseif ($path && strpos('wp-json', $path) !== false) {
return get_site_url($blog_id, $path);
} else {
return $url;
}
}, 10, 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment