0

I am working on a WP system that is supposed to be served by multiple domains. For example, foo.example.com and elpmaxe.com point to the same installation. But depending on the URL, the system will conditionally serve a different landing page.

The question is, how do I make the site_url() just return the current URL, NOT the URL value from the database.

2
  • What is the "current URL" - the one that is being requested in the browser? Perhaps $_SERVER['REQUEST_URI'] will help.
    – Q Studio
    Commented Jan 6, 2021 at 8:54
  • I don't have any problem figuring out the current URL. I have to make WP believe that the site URL is not the one in the database. I want site_url() to return whatever the current URL is.
    – arxoft
    Commented Jan 6, 2021 at 9:34

1 Answer 1

0

You can filter site_url using a filter - called site_url

The hook has the following signature:

apply_filters( 'site_url', string $url, string $path, string|null $scheme, int|null $blog_id )

You can use it like this:

add_filter( 'site_url', 'wpse_381006_custom_site_url', 10, 1 );

function wpse_381006_custom_site_url( $url ){
    if( is_admin() ) // you probably don't want this in admin side
        return $url;

    // for example, return the request_uri - but this is not a complete solution, you would need to work out what you return and in what conditions..
    return $_SERVER['REQUEST_URI'];

}

Reference: https://developer.wordpress.org/reference/hooks/site_url/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.