Frage

Working on my first decoupled site. Backend is handled by WordPress and lives on the subdomain admin.mydomain.com and the frontend lives at mydomain.com.

The first issue I saw with the headless approach, was that all permalinks referred to admin.mydomain.com. I wanted to change that so that the backend referenced to my frontend.

So I changed my config from:
WP_HOME='http://admin.mydomain.test'
WP_SITEURL='http://admin.domain.test/wp'

to:
WP_HOME='http://mydomain.test'
WP_SITEURL='http://admin.domain.test/wp'

which sorted my issue with the permalinks, but now I'm getting console errors on the WP dashboard because the plugins can't find the resources they need, for example ACF:

index.php:63 GET http://mydomain.test/app/plugins/advanced-custom-fields-pro/assets/css/acf-global.css?ver=5.8.4 net::ERR_NAME_NOT_RESOLVED

What would be the proper course of action to fix this?

War es hilfreich?

Lösung

My quick and dirty fix looks like this (please note that I use Roots Bedrock, so it differs from a vanilla WP install).

  1. In .env add this line WP_HOME_ADMIN='http://admin.mydomain.test'

  2. In application.php add this line, preferable just after where WP_HOME is defined: Config::define('WP_HOME_ADMIN', env('WP_HOME_ADMIN'));

  3. Somewhere after where CONTENT_DIR is defined, add this line: Config::define('WP_PLUGIN_URL', Config::get('WP_HOME_ADMIN') . Config::get('CONTENT_DIR') .'/plugins');

Andere Tipps

I understand the question and answer are from more than a year ago, but I just wanted to add some additional information in hopes it might be useful for future visitors of this Question.


Recently, while setting up a Headless WordPress CMS with Roots Bedrock, I came across the same issue and solved it with a slightly different version of your solution. I applied points 1 and 2 exactly the same, but did 3 a bit different. Instead of adding the new line, I changed the declaration of the WP_CONTENT_URL constant from this:

Config::define('WP_CONTENT_URL', Config::get('WP_HOME') . Config::get('CONTENT_DIR'));

to this:

Config::define('WP_CONTENT_URL', Config::get('WP_HOME_ADMIN') . Config::get('CONTENT_DIR'));

This solved the backend plugins paths and also the active theme's backend assets paths.


But there was one final error: The WordPress REST API endpoint wasn't accessible in the backend, because it was being generated with the get_home_url() function, which returns the REST API endpoint appended to the frontend URL, instead of the get_site_url().

Looking for solutions for fixing the REST API URL I came across this post answer where the post author provides this filter function you can paste into your active theme's functions.php file for replacing the REST API URL's base with that of the backend URL:

/*
 * Fix Roots Bedrock WP REST API base endpoint
 */
add_filter( 'rest_url', function( $url ) {
    $pattern = '/(\S+)(\/wp\/?)$/';
    $siteURL = preg_replace( $pattern, '${1}', site_url() );
    $url = str_replace( home_url(), $siteURL, $url );
    
    return $url;
} );

The function replaces the Frontend URL with the Backend URL, and also removes the /wp part of the Roots Bedrock WordPress backend URL.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit wordpress.stackexchange
scroll top