wppaste
WordPress

wp_privacy_anonymize_ip( string $ip_addr, bool $ipv6_fallback = false ): string

Since
4.9.6
Source
wp-includes/functions.php:8383
Returns an anonymized IPv4 or IPv6 address.

Compatibility

WordPress
since 4.9.6
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$ip_addrstring
The IPv4 or IPv6 address to be anonymized.
$ipv6_fallbackbooloptional
Whether to return the original IPv6 address if the needed functions to anonymize it are not present. Default false, return :: (unspecified address).Default: false

Return value

string
The anonymized IP address.

Performance profile

How much work a call to wp_privacy_anonymize_ip() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
5–62

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 81.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_privacy_anonymize_ip() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($ip_addr)5none
!empty($ip_addr)18–48substr_count(), substr_count()
!empty($ip_addr)28–38substr_count(), substr_count(), strrpos()
!empty($ip_addr) && !$ip_addr45–62substr_count(), substr_count(), inet_pton(), inet_pton(), inet_ntop()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev815–6212
8.5815–6212
8.4815–621227 fewer instructions than PHP 8.3
8.31085–8612
8.21085–8612
8.11085–8612
7.41085–8612

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Used by · 2

Source code

function wp_privacy_anonymize_ip( $ip_addr, $ipv6_fallback = false ) {	if ( empty( $ip_addr ) ) {		return '0.0.0.0';	} 	// Detect what kind of IP address this is.	$ip_prefix = '';	$is_ipv6   = substr_count( $ip_addr, ':' ) > 1;	$is_ipv4   = ( 3 === substr_count( $ip_addr, '.' ) ); 	if ( $is_ipv6 && $is_ipv4 ) {		// IPv6 compatibility mode, temporarily strip the IPv6 part, and treat it like IPv4.		$ip_prefix = '::ffff:';		$ip_addr   = preg_replace( '/^\[?[0-9a-f:]*:/i', '', $ip_addr );		$ip_addr   = str_replace( ']', '', $ip_addr );		$is_ipv6   = false;	} 	if ( $is_ipv6 ) {		// IPv6 addresses will always be enclosed in [] if there's a port.		$left_bracket  = strpos( $ip_addr, '[' );		$right_bracket = strpos( $ip_addr, ']' );		$percent       = strpos( $ip_addr, '%' );		$netmask       = 'ffff:ffff:ffff:ffff:0000:0000:0000:0000'; 		// Strip the port (and [] from IPv6 addresses), if they exist.		if ( false !== $left_bracket && false !== $right_bracket ) {			$ip_addr = substr( $ip_addr, $left_bracket + 1, $right_bracket - $left_bracket - 1 );		} elseif ( false !== $left_bracket || false !== $right_bracket ) {			// The IP has one bracket, but not both, so it's malformed.			return '::';		} 		// Strip the reachability scope.		if ( false !== $percent ) {			$ip_addr = substr( $ip_addr, 0, $percent );		} 		// No invalid characters should be left.		if ( preg_match( '/[^0-9a-f:]/i', $ip_addr ) ) {			return '::';		} 		// Partially anonymize the IP by reducing it to the corresponding network ID.		if ( function_exists( 'inet_pton' ) && function_exists( 'inet_ntop' ) ) {			$ip_addr = inet_ntop( inet_pton( $ip_addr ) & inet_pton( $netmask ) );			if ( false === $ip_addr ) {				return '::';			}		} elseif ( ! $ipv6_fallback ) {			return '::';		}	} elseif ( $is_ipv4 ) {		// Strip any port and partially anonymize the IP.		$last_octet_position = strrpos( $ip_addr, '.' );		$ip_addr             = substr( $ip_addr, 0, $last_octet_position ) . '.0';	} else {		return '0.0.0.0';	} 	// Restore the IPv6 prefix to compatibility mode addresses.	return $ip_prefix . $ip_addr;}

Changelog

Introduced in 4.9.6. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.