Custom Drupal Filter to Remove HTTP Protocol

Last modified: 
Thursday, October 29th, 2015

This is an example of a custom Drupal 7 input filter. This example will locate user content featuring images (etc) where the src attribute begins with http: and then remove the protocol portions. For example, <img src="" alt=""> would become <img src="//" alt="">, which allows the server to handle the request however it prefers. This came in handy for converting a large site over to https only, because it had a whole lot of embedded assets that would be impractical to update manually.

 * Define custom input filters for our site.
function MYMODULE_filter_info() {
  $filters = array();
  $filters['no-http'] = array(
    'title' => t('Strip http: protocol from src attributes. I should come after all other filters.'),
    'process callback' => '_MYMODULE_no_http_filter',
  return $filters;

 * This input filter strips http from any src attributes. We needed this because
 * the site owner has hotlinked and embedded content from all over the place
 * and tracking this all down might be an insurmountable challenge to the client.
function _MYMODULE_no_http_filter($text, $filter, $format, $langcode, $cache, $cache_id) {
  return str_ireplace('src="http:', 'src="', $text);

The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.