最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - Hook to change the site URL

programmeradmin0浏览0评论

I am working on a WP system that is supposed to be served by multiple domains. For example, foo.example and elpmaxe 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.

I am working on a WP system that is supposed to be served by multiple domains. For example, foo.example and elpmaxe 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.

Share Improve this question asked Jan 6, 2021 at 7:35 arxoftarxoft 1176 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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/reference/hooks/site_url/

发布评论

评论列表(0)

  1. 暂无评论