The kses_allowed_protocols WordPress PHP Filter allows you to modify the list of protocols that are allowed in HTML attributes.
Usage
add_filter('kses_allowed_protocols', 'my_custom_allowed_protocols');
function my_custom_allowed_protocols($protocols) {
// your custom code here
return $protocols;
}
Parameters
- $protocols (string[]): An array of allowed protocols such as ‘http’, ‘ftp’, ‘tel’, and more.
More information
See WordPress Developer Resources: kses_allowed_protocols
Examples
Add a Custom Protocol
Add a custom protocol, “myprotocol”, to the allowed list of protocols.
add_filter('kses_allowed_protocols', 'add_myprotocol');
function add_myprotocol($protocols) {
$protocols[] = 'myprotocol';
return $protocols;
}
Remove a Protocol
Remove the “ftp” protocol from the allowed list of protocols.
add_filter('kses_allowed_protocols', 'remove_ftp_protocol');
function remove_ftp_protocol($protocols) {
$key = array_search('ftp', $protocols);
if ($key !== false) {
unset($protocols[$key]);
}
return $protocols;
}
Allow Only Specific Protocols
Allow only “http” and “https” protocols.
add_filter('kses_allowed_protocols', 'allow_only_http_https');
function allow_only_http_https($protocols) {
return array('http', 'https');
}
Add Multiple Custom Protocols
Add multiple custom protocols to the allowed list of protocols.
add_filter('kses_allowed_protocols', 'add_multiple_protocols');
function add_multiple_protocols($protocols) {
$new_protocols = array('myprotocol1', 'myprotocol2', 'myprotocol3');
return array_merge($protocols, $new_protocols);
}
Clear All Allowed Protocols
Remove all protocols from the allowed list of protocols.
add_filter('kses_allowed_protocols', 'clear_allowed_protocols');
function clear_allowed_protocols($protocols) {
return array();
}