{"id":19008289,"date":"2026-09-01T09:15:35","date_gmt":"2026-09-01T09:15:35","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/avoid-php-warning-when-remote_addr-is-unavailable\/"},"modified":"2026-09-01T09:16:18","modified_gmt":"2026-09-01T09:16:18","slug":"avoid-php-warning-when-remote_addr-is-unavailable","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/avoid-php-warning-when-remote_addr-is-unavailable\/","title":{"rendered":"Avoid PHP warning when REMOTE_ADDR is unavailable"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Title: Avoid PHP warning when REMOTE_ADDR is unavailable<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The plugin logs every outgoing email through the <code>wp_mail<\/code> filter. This filter can also run outside a normal HTTP request, for example from WP-CLI, a server-side cron job, or another background process.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In these contexts, <code>$_SERVER['REMOTE_ADDR']<\/code> may not be defined. The current direct array access in <code>inc\/email-logging.php<\/code> produces a PHP warning:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PHP Warning: Undefined array key &#8220;REMOTE_ADDR&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The warning occurs before <code>sanitize_text_field()<\/code> can process the value, because the array key is accessed first. Email logging itself can continue, but the warning adds unnecessary noise to the PHP error log.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suggested fix:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>$log['ip_address'] = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ?? '' );<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Alternatively, use an explicit fallback such as&nbsp;<code>UNKNOWN<\/code>&nbsp;if the log should distinguish an unavailable address from an intentionally empty value:<br \/><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>$log['ip_address'] = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ?? 'UNKNOWN' );<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br \/>This preserves the IP address for regular HTTP requests while allowing email logging to work cleanly in non-HTTP execution contexts.<br \/><\/p>\n","protected":false},"template":"","class_list":["post-19008289","topic","type-topic","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/19008289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":1,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/19008289\/revisions"}],"predecessor-version":[{"id":19008291,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/19008289\/revisions\/19008291"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=19008289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}