‘pre_user_url’ is a WordPress filter that lets you modify a user’s URL before the user is created or updated.
This can be useful for sanitizing or customizing the URL in various ways.
Usage
To use the filter, add the following code to your theme’s functions.php
file or a custom plugin:
add_filter('pre_user_url', 'your_function_name', 10, 1); function your_custom_function( $raw_user_url ) { // Your custom code here }
Parameters
- $raw_user_url (string) – The user’s URL that you want to filter.
Examples
Make all URLs HTTPS
function make_https($raw_user_url) { return preg_replace('/^http:/', 'https:', $raw_user_url); } add_filter('pre_user_url', 'make_https', 10, 1);
This code example replaces the “http” protocol with “https” for all user URLs.
Add www to URLs
function add_www($raw_user_url) { return preg_replace('/^https?:///', '$0www.', $raw_user_url); } add_filter('pre_user_url', 'add_www', 10, 1);
This code example adds “www.” to the beginning of user URLs if it’s not already there.
Remove trailing slashes from URLs
function remove_trailing_slash($raw_user_url) { return rtrim($raw_user_url, '/'); } add_filter('pre_user_url', 'remove_trailing_slash', 10, 1);
This code example removes any trailing slashes from the user URLs.
Remove query strings from URLs
function remove_query_strings($raw_user_url) { return strtok($raw_user_url, '?'); } add_filter('pre_user_url', 'remove_query_strings', 10, 1);
This code example removes query strings from the user URLs, keeping only the base URL.
Replace certain domain with another
function replace_domain($raw_user_url) { return str_replace('olddomain.com', 'newdomain.com', $raw_user_url); } add_filter('pre_user_url', 'replace_domain', 10, 1);
This code example replaces “olddomain.com” with “newdomain.com” in the user URLs.