The next hack requires the use of jQuery , jQuery UI, and the Mike Alsup jQuery Form Plugin to load AJAX files. The hack works with related versions (jQ 1.7.2 and jQUI 1.8.20). I can not guarantee compatibility with other versions.
In your <head> you will need to specify the dependencies:
<script type='text/javascript' src='jquery.min.js'></script> <link href='theme/jquery-ui.css' rel='stylesheet' type='text/css' /> <script type='text/javascript' src='jquery-ui.js'></script> <script type='text/javascript' src='wmd/showdown.js'></script> <script type='text/javascript' src='wmd/wmd.js'></script> <link type='text/css' rel='stylesheet' href='wmd/wmd.css'/> <script type='text/javascript' src='jquery.form.js'></script>
In fact, we need to make one change to wmd.js
Continue and find (ctrl + f) for var form = doc.createElement("form");
Immediately after this line, assign an id form, dialogform will do: form.id = "dialogform";
Now at the front end run:
$(document).ready(function(){ $("#wmd-image-button").live("click",function(){ setTimeout(function(){ $(".wmd-prompt-dialog").css({"opacity": "0", display: "none"}); }, 100); var $div = $("<div>"); var $form = $("<form>").attr({action: "submit_image.php", method: "post"}) var $file = $("<input/>").attr({type: "file", name: "image"}); var $name = $("<input/>").attr({type: "text", name: "name", placeholder: "Name"}); var $submit = $("<input/>").attr("type", "submit"); $form.append($name, $file, $submit).ajaxForm(function(r) { r = $.parseJSON(r); if(r.success){ $("#dialogform input[type='text']").val(r.filename); $("#dialogform input[value='OK']").trigger("click"); $div.dialog("close"); } }); $div.append($form).dialog({title: "Upload Image"}); }); $("#wmd-link-button").live("click", function(){ setTimeout(function(){ $(".wmd-prompt-dialog").css("opacity", "1"); }, 100); }); });
Remember that the message was written for jQuery 1.7.2, and live() has been deprecated since then. Switch to on() if you are using a newer version of jQuery
And on the server, in submit_image.php :
$f = $_FILES['image']; $p = $_POST; $allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF); $detectedType = exif_imagetype($f['tmp_name']); if(in_array($detectedType, $allowedTypes)){ $pi = pathinfo($f['name']); $ext = $pi['extension']; $target = "img/" . strtolower(str_replace(" ", "-", $p['name'])) . "." . $ext; if(move_uploaded_file($f['tmp_name'], $target)){ $returnArr = array( "success" => true, "filename" => site_url($target) ); echo json_encode($returnArr); } else echo json_encode(array("success" => false)); } else echo json_encode(array("success" => false, "msg" => "Invalid File Type."));
Hope this helps you get started. It was written a couple of years ago when my javascript skills were lower! Haha I used to do this on a blog (which is now dead), with step-by-step instructions and explanations; a lot of unnecessary fluff. Thanks @Kamiccolo for bringing this link to my attention. I needed to consult a machine to revive it.