jerturowetz
2/17/2017 - 9:44 PM

Strips length & width from editor when embedding images

Strips length & width from editor when embedding images

<?php
/*
Plugin Name: Fix Media
Description: <strong>Fixes media functions:</strong>: removes URL root when embedding, removes width and height, sets the default embed type to 'none' instead of 'link'
Author: Jeremy Turowetz
Version: 1.0
Author URI: http://jeremy.turowetz.com/
License: GPLv3 or later
*/

defined('ABSPATH') or die("No script kiddies please!");

// Remove the URL root when embedding images
add_filter('image_send_to_editor','image_to_relative',5,8);

function image_to_relative($html, $id, $caption, $title, $align, $url, $size, $alt)
{
  $sp = strpos($html,"src=") + 5;
  $ep = strpos($html,"\"",$sp);

  $imageurl = substr($html,$sp,$ep-$sp);

  $relativeurl = str_replace("http://","",$imageurl);
  $sp = strpos($relativeurl,"/");
  $relativeurl = substr($relativeurl,$sp);

  $html = str_replace($imageurl,$relativeurl,$html);

  return $html;
}


// Remove image width and height
add_filter( 'post_thumbnail_html', 'remove_image_height_width', 10 );
add_filter( 'image_send_to_editor', 'remove_image_height_width', 10 );

function remove_image_height_width( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;
}


// Set the default embed type to 'none' instead of 'link'
add_action('admin_init', 'wpb_imagelink_setup', 10);

function wpb_imagelink_setup() {
  $image_set = get_option( 'image_default_link_type' );

  if ($image_set !== 'none') {
    update_option('image_default_link_type', 'none');
  }
}