Skip to content

Instantly share code, notes, and snippets.

@Mosharush
Created October 17, 2019 10:40
Show Gist options
  • Save Mosharush/b35ba3bb16fba05927b40d38521f594d to your computer and use it in GitHub Desktop.
Save Mosharush/b35ba3bb16fba05927b40d38521f594d to your computer and use it in GitHub Desktop.
This plugin resolve broken layout when we work on local machine without all uploaded images up to date. Will replace the images with image placeholder via placeholder.com
<?php
/**
* @package Auto_Placeholder_Images
* @version 1.0.0
*/
/*
Plugin Name: Auto Placeholder For Broken Images
Plugin URI: https://www.webstorm.co.il/css-live-reload/
Description: **FOR DEV USE ONLY** This plugin resolve broken layout when we work on local machine without all uploaded images up to date. Will replace the images with image placeholder via placeholder.com
Author: Moshe Harush
Version: 1.0.0
Author URI: https://www.webstorm.co.il
*/
function ws_try_mock_404_images() {
if (is_404()) {
if (preg_match('#((\d{1,4})?x(\d{1,4})?)?\.(png|jpg|gif|svg)#', $_SERVER['REQUEST_URI'], $matches)) {
if ($matches[1]) {
$location = "https://via.placeholder.com/" . $matches[1];
} else {
$location = "https://via.placeholder.com/950x475";
}
header("Location: " . $location);
}
}
}
add_action('template_redirect', 'ws_try_mock_404_images');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment