我已经在wordpress帖子表单中添加了自定义功能,其中生成了画布图像,并将其传递给输入,然后我正在尝试保存它。但当我点击“更新”按钮时,我会看到404错误页面。
在函数中。我的主题php我有:
add_action( \'edit_form_advanced\', \'addHtmlToForm\' );
function addHTMLToForm(){
?>
<input type="hidden" name="myimage" id="myImage" value="" />
<canvas width="700" height="700" id="mainImage"></canvas>
<script>
var img = new Image;
var mainCanvas = document.getElementById(\'mainImage\');
img.crossOrigin = "anonymous";
img.onload = function(){
ctx.drawImage(img,0,0);
jQuery(\'#yImage\').val(mainCanvas.toDataURL(\'image/png\'));
};
img.src = \'https://source.to.my.image.png\';
</script>
<?php
}
然后通过函数保存数据
add_action( \'save_post\', \'saveExtraFields\', 10, 3);
function saveExtraFields($ID){
global $post, $wpdb;
$mapFileName = false;
if(isset($_POST[\'myimage\']) && $_POST[\'myimage\'] != \'\'){
$uploadDir = $_SERVER[\'DOCUMENT_ROOT\'].\'/uploads/products/\';
$mapFileName = "{$ID}.png";
$img = $_POST[\'myimage\'];
$img = str_replace(\'data:image/png;base64,\', \'\', $img);
$img = str_replace(\' \', \'+\', $img);
$data = base64_decode($img);
$file = $uploadDir.$mapFileName;
file_put_contents($file, $data);
}
}