Using WordPress ‘home_url’ PHP filter

The home_url WordPress PHP Filter allows you to modify the home URL of your WordPress site.

Usage

add_filter('home_url', 'your_custom_function', 10, 4);

function your_custom_function($url, $path, $orig_scheme, $blog_id) {
    // your custom code here
    return $url;
}

Parameters

  • $url (string): The complete home URL including scheme and path.
  • $path (string): Path relative to the home URL. Blank string if no path is specified.
  • $orig_scheme (string|null): Scheme to give the home URL context. Accepts ‘http’, ‘https’, ‘relative’, ‘rest’, or null.
  • $blog_id (int|null): Site ID, or null for the current site.

More information

See WordPress Developer Resources: home_url

Examples

Change the home URL scheme to HTTPS

Forces the home URL to always use the ‘https’ scheme.

add_filter('home_url', 'force_https_scheme', 10, 4);

function force_https_scheme($url, $path, $orig_scheme, $blog_id) {
    $url = set_url_scheme($url, 'https');
    return $url;
}

Add a custom prefix to the home URL

Adds a custom prefix to the home URL.

add_filter('home_url', 'add_custom_prefix', 10, 4);

function add_custom_prefix($url, $path, $orig_scheme, $blog_id) {
    $url = 'https://myprefix.' . $url;
    return $url;
}

Change the home URL path

Changes the path of the home URL to a custom path.

add_filter('home_url', 'change_home_url_path', 10, 4);

function change_home_url_path($url, $path, $orig_scheme, $blog_id) {
    $url = trailingslashit($url) . 'custom-path';
    return $url;
}

Redirect the home URL to another site

Redirects the home URL to another website.

add_filter('home_url', 'redirect_home_url', 10, 4);

function redirect_home_url($url, $path, $orig_scheme, $blog_id) {
    $url = 'https://www.example.com';
    return $url;
}

Add a language code to the home URL

Appends a language code to the home URL.

add_filter('home_url', 'add_language_code', 10, 4);

function add_language_code($url, $path, $orig_scheme, $blog_id) {
    $url = trailingslashit($url) . 'en';
    return $url;
}