Hook to change the site URL

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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/


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x