Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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