Skip to content

Instantly share code, notes, and snippets.

@yokotak0527
Last active November 4, 2019 08:07
Show Gist options
  • Save yokotak0527/2b181e16dea6a276ee4de6306d5a9190 to your computer and use it in GitHub Desktop.
Save yokotak0527/2b181e16dea6a276ee4de6306d5a9190 to your computer and use it in GitHub Desktop.
WordPresの画像をエディタに追加する際 HTML ではなく Markdown にする。
<?php
function change_image_insert_code($html){
$rootURL = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'];
preg_match('/src="([a-z\d:\/.\-\_%~^]+)/i', $html, $src);
$src = str_replace($rootURL, '', $src[1]);
// error_log($html, 3, '/var/www/html/dump.log');
preg_match('/alt="([^"]+)"/', $html, $alt);
$alt = empty($alt[1]) ? '' : $alt[1];
preg_match('/title="([^"]+)"/', $html, $title);
$title = empty($title[1]) ? '' : $title[1];
return $title ? "![{$alt}]({$src} \"$title\")" : "![{$alt}]({$src})";
}
add_filter('image_send_to_editor', 'change_image_insert_code', 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment