Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save guytzhak/9dcfbb94953cd4a83c9bddcfef08c7b4 to your computer and use it in GitHub Desktop.
Save guytzhak/9dcfbb94953cd4a83c9bddcfef08c7b4 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