钩子文档

http_allowed_safe_ports

💡 云策文档标注

概述

http_allowed_safe_ports 是一个 WordPress 过滤器,用于控制 HTTP API 中视为安全的端口列表。它允许开发者修改默认的安全端口,以允许外部请求。

关键要点

  • 此过滤器用于定义 HTTP API 中允许的安全端口数组。
  • 默认安全端口包括 80、443 和 8080。
  • 可以通过 add_filter 钩子自定义端口列表,例如添加端口 3000。
  • 相关函数 wp_http_validate_url() 使用此过滤器验证 URL 安全性。
  • 自 WordPress 5.9.0 版本引入。

代码示例

add_filter( 'http_allowed_safe_ports', 'wpdocs_allowed_ports' );

function wpdocs_allowed_ports( $ports ) {
    $ports = array( 80, 443, 8080, 3000 );

    return $ports;
}

📄 原文内容

Controls the list of ports considered safe in HTTP API.

Description

Allows to change and allow external requests for the HTTP request.

Parameters

$allowed_portsint[]
Array of integers for valid ports. Default allowed ports are 80, 443, and 8080.
$hoststring
Host name of the requested URL.
$urlstring
Requested URL.

Source

$allowed_ports = apply_filters( 'http_allowed_safe_ports', array( 80, 443, 8080 ), $host, $url );

Changelog

Version Description
5.9.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    By default, WordPress considers ports 80, 8080 and 443 as “safe”.

    Example code snippet of changing allowed ports to 80, 8080, 443 and 3000:

    add_filter( 'http_allowed_safe_ports', 'wpdocs_allowed_ports' );
    
    function wpdocs_allowed_ports( $ports ) {
        $ports = array( 80, 443, 8080, 3000 );
    
        return $ports;
    }