2020-01-27 15:28:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2020-01-29 04:41:36 +01:00
|
|
|
*Ajax操作文件
|
2020-01-27 15:28:34 +01:00
|
|
|
*/
|
|
|
|
|
2020-01-27 19:03:14 +01:00
|
|
|
function gdk_test_email() {
|
2020-01-27 15:28:34 +01:00
|
|
|
$is_error = !wp_mail('donotreply@mywpku.com', '测试发信', 'WP 积木测试发信');
|
|
|
|
if ($is_error) {
|
|
|
|
exit('0');
|
|
|
|
}else{
|
|
|
|
exit('1');
|
|
|
|
}
|
|
|
|
}
|
2020-01-27 19:03:14 +01:00
|
|
|
add_action('wp_ajax_nopriv_gdk_test_email', 'gdk_test_email');
|
|
|
|
add_action('wp_ajax_gdk_test_email', 'gdk_test_email');
|
|
|
|
|
|
|
|
|
2020-01-30 09:47:37 +01:00
|
|
|
//粘贴上传图片
|
|
|
|
function gdk_pasteup_imag() {
|
|
|
|
if($_FILES) {
|
|
|
|
global $post;
|
|
|
|
$post_ID = $post->ID;
|
|
|
|
$wp_upload_dir = wp_upload_dir();
|
|
|
|
$file = $_FILES["imageFile"];
|
|
|
|
$result=array ("success"=>false,"message"=>"Null");
|
|
|
|
if (in_array($file["type"],array ("image/gif","image/jpeg","image/pjpeg","image/png"))) {
|
|
|
|
if ($file["error"]>0) {
|
|
|
|
$result['message']="error";
|
|
|
|
} else {
|
|
|
|
$file_name = md5_file($file["tmp_name"]).str_replace("image/",".",$file["type"]);//img name
|
|
|
|
$file_url = $wp_upload_dir['url']."/".$file_name;
|
|
|
|
$file_path = $wp_upload_dir['path']."/".$file_name;
|
|
|
|
if (!file_exists($file_path)) {
|
|
|
|
move_uploaded_file($file["tmp_name"],$file_path);
|
|
|
|
$attachment = [
|
|
|
|
'guid' => $wp_upload_dir['url'] . '/' . basename( $file_path ),
|
|
|
|
'post_mime_type' => $file['type'],
|
|
|
|
'post_title' => $file_name,
|
|
|
|
'post_content' => '',
|
|
|
|
'post_status' => 'inherit'
|
|
|
|
];
|
|
|
|
$attach_id = wp_insert_attachment( $attachment, $file_name, $post_ID);
|
|
|
|
//这是wp内置的上传附件的函数
|
|
|
|
require_once( ABSPATH . 'wp-admin/includes/image.php' );
|
|
|
|
$attach_data = wp_generate_attachment_metadata( $attach_id, $file_path );
|
|
|
|
wp_update_attachment_metadata( $attach_id, $attach_data );
|
|
|
|
}
|
|
|
|
$result['success']= true;
|
|
|
|
$result['message']= $file_url;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result['message']="Invalid file";
|
|
|
|
}
|
|
|
|
echo(json_encode($result));
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_action('wp_ajax_nopriv_gdk_pasteup_imag', 'gdk_pasteup_imag');
|
|
|
|
add_action('wp_ajax_gdk_pasteup_imag', 'gdk_pasteup_imag');
|