我的WordPress function.php
文件中有图像上传功能,但我的function.php
文件是用离子立方体编码器编码的,所以我无法编辑该功能。
但我有一个完整的代码,由我的主题作者给出。并且作者告诉我覆盖它将起作用的功能。
我的函数名称为pinc_upload_pin()
,并添加为add_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
现在我需要覆盖上面的功能。
我试过创建一个单独的文件名function_new.php并将其包含在header.php
中,就像使用require_once
一样。我使用remove_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
并添加我自己的函数并挂钩它。以下是我的function_new.php
代码
remove_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
function pinc_upload_pin_new(){
check_ajax_referer('upload_pin', 'ajax-nonce');
do_action('pinc_before_upload_pin', $_POST);
$minWidth = 2;
$minHeight = 2;
$minWidth = apply_filters('pinc_minwidth', $minWidth);
$minHeight = apply_filters('pinc_minheight', $minHeight);
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
if ($_POST['mode'] == 'computer') {
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$imageTypes = array (
1, //IMAGETYPE_GIF
2, //IMAGETYPE_JPEG
3 //IMAGETYPE_PNG
);
$imageinfo = getimagesize($_FILES[$file]['tmp_name']);
$width = @$imageinfo[0];
$height = @$imageinfo[1];
$type = @$imageinfo[2];
$mime = @$imageinfo['mime'];
if (!in_array($type, $imageTypes)) {
@unlink($_FILES[$file]['tmp_name']);
echo 'error';
die();
}
if ($width < $minWidth || $height < $minWidth) {
@unlink($_FILES[$file]['tmp_name']);
echo 'errorsize';
die();
}
if($mime != 'image/gif' && $mime != 'image/jpeg' && $mime != 'image/png') {
@unlink($_FILES[$file]['tmp_name']);
echo 'error';
die();
}
switch($type) {
case 1:
$ext = '.gif';
//check if is animated gif
$frames = 0;
if(($fh = @fopen($_FILES[$file]['tmp_name'], 'rb')) && $error != 'error') {
while(!feof($fh) && $frames < 2) {
$chunk = fread($fh, 1024 * 100); //read 100kb at a time
$frames += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
}
}
fclose($fh);
break;
case 2:
$ext = '.jpg';
break;
case 3:
$ext = '.png';
break;
}
$transliterationTable = array('á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A', 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A', 'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C', 'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C', 'ď' => 'd', 'Ď' => 'D', 'ḋ' => 'd', 'Ḋ' => 'D', 'đ' => 'd', 'Đ' => 'D', 'ð' => 'dh', 'Ð' => 'Dh', 'é' => 'e', 'É' => 'E', 'è' => 'e', 'È' => 'E', 'ĕ' => 'e', 'Ĕ' => 'E', 'ê' => 'e', 'Ê' => 'E', 'ě' => 'e', 'Ě' => 'E', 'ë' => 'e', 'Ë' => 'E', 'ė' => 'e', 'Ė' => 'E', 'ę' => 'e', 'Ę' => 'E', 'ē' => 'e', 'Ē' => 'E', 'ḟ' => 'f', 'Ḟ' => 'F', 'ƒ' => 'f', 'Ƒ' => 'F', 'ğ' => 'g', 'Ğ' => 'G', 'ĝ' => 'g', 'Ĝ' => 'G', 'ġ' => 'g', 'Ġ' => 'G', 'ģ' => 'g', 'Ģ' => 'G', 'ĥ' => 'h', 'Ĥ' => 'H', 'ħ' => 'h', 'Ħ' => 'H', 'í' => 'i', 'Í' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I', 'ï' => 'i', 'Ï' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', 'į' => 'i', 'Į' => 'I', 'ī' => 'i', 'Ī' => 'I', 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ĺ' => 'l', 'Ĺ' => 'L', 'ľ' => 'l', 'Ľ' => 'L', 'ļ' => 'l', 'Ļ' => 'L', 'ł' => 'l', 'Ł' => 'L', 'ṁ' => 'm', 'Ṁ' => 'M', 'ń' => 'n', 'Ń' => 'N', 'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ņ' => 'n', 'Ņ' => 'N', 'ó' => 'o', 'Ó' => 'O', 'ò' => 'o', 'Ò' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ő' => 'o', 'Ő' => 'O', 'õ' => 'o', 'Õ' => 'O', 'ø' => 'oe', 'Ø' => 'OE', 'ō' => 'o', 'Ō' => 'O', 'ơ' => 'o', 'Ơ' => 'O', 'ö' => 'oe', 'Ö' => 'OE', 'ṗ' => 'p', 'Ṗ' => 'P', 'ŕ' => 'r', 'Ŕ' => 'R', 'ř' => 'r', 'Ř' => 'R', 'ŗ' => 'r', 'Ŗ' => 'R', 'ś' => 's', 'Ś' => 'S', 'ŝ' => 's', 'Ŝ' => 'S', 'š' => 's', 'Š' => 'S', 'ṡ' => 's', 'Ṡ' => 'S', 'ş' => 's', 'Ş' => 'S', 'ș' => 's', 'Ș' => 'S', 'ß' => 'SS', 'ť' => 't', 'Ť' => 'T', 'ṫ' => 't', 'Ṫ' => 'T', 'ţ' => 't', 'Ţ' => 'T', 'ț' => 't', 'Ț' => 'T', 'ŧ' => 't', 'Ŧ' => 'T', 'ú' => 'u', 'Ú' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ŭ' => 'u', 'Ŭ' => 'U', 'û' => 'u', 'Û' => 'U', 'ů' => 'u', 'Ů' => 'U', 'ű' => 'u', 'Ű' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ų' => 'u', 'Ų' => 'U', 'ū' => 'u', 'Ū' => 'U', 'ư' => 'u', 'Ư' => 'U', 'ü' => 'ue', 'Ü' => 'UE', 'ẃ' => 'w', 'Ẃ' => 'W', 'ẁ' => 'w', 'Ẁ' => 'W', 'ŵ' => 'w', 'Ŵ' => 'W', 'ẅ' => 'w', 'Ẅ' => 'W', 'ý' => 'y', 'Ý' => 'Y', 'ỳ' => 'y', 'Ỳ' => 'Y', 'ŷ' => 'y', 'Ŷ' => 'Y', 'ÿ' => 'y', 'Ÿ' => 'Y', 'ź' => 'z', 'Ź' => 'Z', 'ž' => 'z', 'Ž' => 'Z', 'ż' => 'z', 'Ż' => 'Z', 'þ' => 'th', 'Þ' => 'Th', 'µ' => 'u', 'а' => 'a', 'А' => 'a', 'б' => 'b', 'Б' => 'b', 'в' => 'v', 'В' => 'v', 'г' => 'g', 'Г' => 'g', 'д' => 'd', 'Д' => 'd', 'е' => 'e', 'Е' => 'E', 'ё' => 'e', 'Ё' => 'E', 'ж' => 'zh', 'Ж' => 'zh', 'з' => 'z', 'З' => 'z', 'и' => 'i', 'И' => 'i', 'й' => 'j', 'Й' => 'j', 'к' => 'k', 'К' => 'k', 'л' => 'l', 'Л' => 'l', 'м' => 'm', 'М' => 'm', 'н' => 'n', 'Н' => 'n', 'о' => 'o', 'О' => 'o', 'п' => 'p', 'П' => 'p', 'р' => 'r', 'Р' => 'r', 'с' => 's', 'С' => 's', 'т' => 't', 'Т' => 't', 'у' => 'u', 'У' => 'u', 'ф' => 'f', 'Ф' => 'f', 'х' => 'h', 'Х' => 'h', 'ц' => 'c', 'Ц' => 'c', 'ч' => 'ch', 'Ч' => 'ch', 'ш' => 'sh', 'Ш' => 'sh', 'щ' => 'sch', 'Щ' => 'sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'y', 'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'e', 'ю' => 'ju', 'Ю' => 'ju', 'я' => 'ja', 'Я' => 'ja');
$fname = $_FILES[$file]['name'];
$fname = str_replace(array_keys($transliterationTable), array_values($transliterationTable), $fname);
$filename = time() . str_shuffle('pcl48');
$original_filename = preg_replace('/[^(\x20|\x61-\x7A)]*/', '', strtolower(str_ireplace($ext, '', $fname))); //preg_replace('/[^(\x48-\x7A)]*/' strips non-utf character. Ref: http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0000
$_FILES[$file]['name'] = strtolower(substr($original_filename, 0, 100)) . '-' . $filename . $ext;
$attach_id = media_handle_upload($file, $post_id);
if (is_wp_error($attach_id)) {
@unlink($_FILES[$file]['tmp_name']);
echo 'error';
die();
} else {
if ($frames > 1) {
update_post_meta($attach_id, 'a_gif', 'yes');
}
}
}
}
update_post_meta($attach_id, 'pinc_unattached', 'yes');
$return = array();
$thumbnail = wp_get_attachment_image_src($attach_id, 'medium');
$return['thumbnail'] = $thumbnail[0];
$return['id'] = $attach_id;
do_action('pinc_after_upload_pin_computer', $attach_id);
echo json_encode($return);
} else if ($_POST['mode'] == 'web') {
$url = esc_url_raw($_POST['pin_upload_web']);
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$image = curl_exec($ch);
curl_close($ch);
} elseif (ini_get('allow_url_fopen')) {
$image = file_get_contents($url, false, $context);
}
if (!$image) {
echo 'error';
die();
}
$filename = time() . str_shuffle('pcl48');
$file_array['tmp_name'] = WP_CONTENT_DIR . "/" . $filename . '.tmp';
$filetmp = file_put_contents($file_array['tmp_name'], $image);
if (!$filetmp) {
@unlink($file_array['tmp_name']);
echo 'error';
die();
}
$imageTypes = array (
1, //IMAGETYPE_GIF
2, //IMAGETYPE_JPEG
3 //IMAGETYPE_PNG
);
$imageinfo = getimagesize($file_array['tmp_name']);
$width = @$imageinfo[0];
$height = @$imageinfo[1];
$type = @$imageinfo[2];
$mime = @$imageinfo['mime'];
if (!in_array ($type, $imageTypes)) {
@unlink($file_array['tmp_name']);
echo 'error';
die();
}
if ($width < $minWidth || $height < $minWidth) {
@unlink($file_array['tmp_name']);
echo 'errorsize';
die();
}
if($mime != 'image/gif' && $mime != 'image/jpeg' && $mime != 'image/png') {
@unlink($file_array['tmp_name']);
echo 'error';
die();
}
switch($type) {
case 1:
$ext = '.gif';
//check if is animated gif
$frame = 0;
if(($fh = @fopen($file_array['tmp_name'], 'rb')) && $error != 'error') {
while(!feof($fh) && $frames < 2) {
$chunk = fread($fh, 1024 * 100); //read 100kb at a time
$frames += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
}
}
fclose($fh);
break;
case 2:
$ext = '.jpg';
break;
case 3:
$ext = '.png';
break;
}
$original_filename = preg_replace('/[^(\x20|\x61-\x7A)]*/', '', strtolower(str_ireplace($ext, '', basename($url)))); //preg_replace('/[^(\x48-\x7A)]*/' strips non-utf character. Ref: http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0000
$file_array['name'] = strtolower(substr($original_filename, 0, 100)) . '-' . $filename . $ext;
$attach_id = media_handle_sideload($file_array, $post_id);
if (is_wp_error($attach_id)) {
@unlink($file_array['tmp_name']);
echo 'error';
die();
} else {
if ($frames > 1) {
update_post_meta($attach_id, 'a_gif', 'yes');
}
}
update_post_meta($attach_id, 'pinc_unattached', 'yes');
$return = array();
$thumbnail = wp_get_attachment_image_src($attach_id, 'medium');
$return['thumbnail'] = $thumbnail[0];
$return['id'] = $attach_id;
do_action('pinc_after_upload_pin_web', $attach_id);
echo json_encode($return);
}
exit;
}
add_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin_new');
但我的这个新功能无法调用。它仍然调用旧函数pinc_upload_pin();
请指导我错过的地方
答案 0 :(得分:0)
如何以比旧函数更高的优先级挂钩新函数,然后删除该函数中的前一个钩子,以便只调用新函数。
未经测试但可能通过为您的钩子提供优先级9(任何小于10的数字,因为默认优先级为10,如果未指定)可能会起作用,如下所示:
add_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin_new',9); // High priority
add_action('wp_ajax_nopriv_pinc-upload-pin', 'pinc_upload_pin_new',9); // Use this line only if you need this hook for non-logged in users as well
function pinc_upload_pin_new(){
remove_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
check_ajax_referer('upload_pin', 'ajax-nonce');
do_action('pinc_before_upload_pin', $_POST);
$minWidth = 2;
$minHeight = 2;
$minWidth = apply_filters('pinc_minwidth', $minWidth);
$minHeight = apply_filters('pinc_minheight', $minHeight);
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
if ($_POST['mode'] == 'computer') {
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$imageTypes = array (
1, //IMAGETYPE_GIF
2, //IMAGETYPE_JPEG
3 //IMAGETYPE_PNG
);
$imageinfo = getimagesize($_FILES[$file]['tmp_name']);
$width = @$imageinfo[0];
$height = @$imageinfo[1];
$type = @$imageinfo[2];
$mime = @$imageinfo['mime'];
if (!in_array($type, $imageTypes)) {
@unlink($_FILES[$file]['tmp_name']);
echo 'error';
die();
}
if ($width < $minWidth || $height < $minWidth) {
@unlink($_FILES[$file]['tmp_name']);
echo 'errorsize';
die();
}
if($mime != 'image/gif' && $mime != 'image/jpeg' && $mime != 'image/png') {
@unlink($_FILES[$file]['tmp_name']);
echo 'error';
die();
}
switch($type) {
case 1:
$ext = '.gif';
//check if is animated gif
$frames = 0;
if(($fh = @fopen($_FILES[$file]['tmp_name'], 'rb')) && $error != 'error') {
while(!feof($fh) && $frames < 2) {
$chunk = fread($fh, 1024 * 100); //read 100kb at a time
$frames += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
}
}
fclose($fh);
break;
case 2:
$ext = '.jpg';
break;
case 3:
$ext = '.png';
break;
}
$transliterationTable = array('á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A', 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A', 'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C', 'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C', 'ď' => 'd', 'Ď' => 'D', 'ḋ' => 'd', 'Ḋ' => 'D', 'đ' => 'd', 'Đ' => 'D', 'ð' => 'dh', 'Ð' => 'Dh', 'é' => 'e', 'É' => 'E', 'è' => 'e', 'È' => 'E', 'ĕ' => 'e', 'Ĕ' => 'E', 'ê' => 'e', 'Ê' => 'E', 'ě' => 'e', 'Ě' => 'E', 'ë' => 'e', 'Ë' => 'E', 'ė' => 'e', 'Ė' => 'E', 'ę' => 'e', 'Ę' => 'E', 'ē' => 'e', 'Ē' => 'E', 'ḟ' => 'f', 'Ḟ' => 'F', 'ƒ' => 'f', 'Ƒ' => 'F', 'ğ' => 'g', 'Ğ' => 'G', 'ĝ' => 'g', 'Ĝ' => 'G', 'ġ' => 'g', 'Ġ' => 'G', 'ģ' => 'g', 'Ģ' => 'G', 'ĥ' => 'h', 'Ĥ' => 'H', 'ħ' => 'h', 'Ħ' => 'H', 'í' => 'i', 'Í' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I', 'ï' => 'i', 'Ï' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', 'į' => 'i', 'Į' => 'I', 'ī' => 'i', 'Ī' => 'I', 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ĺ' => 'l', 'Ĺ' => 'L', 'ľ' => 'l', 'Ľ' => 'L', 'ļ' => 'l', 'Ļ' => 'L', 'ł' => 'l', 'Ł' => 'L', 'ṁ' => 'm', 'Ṁ' => 'M', 'ń' => 'n', 'Ń' => 'N', 'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ņ' => 'n', 'Ņ' => 'N', 'ó' => 'o', 'Ó' => 'O', 'ò' => 'o', 'Ò' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ő' => 'o', 'Ő' => 'O', 'õ' => 'o', 'Õ' => 'O', 'ø' => 'oe', 'Ø' => 'OE', 'ō' => 'o', 'Ō' => 'O', 'ơ' => 'o', 'Ơ' => 'O', 'ö' => 'oe', 'Ö' => 'OE', 'ṗ' => 'p', 'Ṗ' => 'P', 'ŕ' => 'r', 'Ŕ' => 'R', 'ř' => 'r', 'Ř' => 'R', 'ŗ' => 'r', 'Ŗ' => 'R', 'ś' => 's', 'Ś' => 'S', 'ŝ' => 's', 'Ŝ' => 'S', 'š' => 's', 'Š' => 'S', 'ṡ' => 's', 'Ṡ' => 'S', 'ş' => 's', 'Ş' => 'S', 'ș' => 's', 'Ș' => 'S', 'ß' => 'SS', 'ť' => 't', 'Ť' => 'T', 'ṫ' => 't', 'Ṫ' => 'T', 'ţ' => 't', 'Ţ' => 'T', 'ț' => 't', 'Ț' => 'T', 'ŧ' => 't', 'Ŧ' => 'T', 'ú' => 'u', 'Ú' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ŭ' => 'u', 'Ŭ' => 'U', 'û' => 'u', 'Û' => 'U', 'ů' => 'u', 'Ů' => 'U', 'ű' => 'u', 'Ű' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ų' => 'u', 'Ų' => 'U', 'ū' => 'u', 'Ū' => 'U', 'ư' => 'u', 'Ư' => 'U', 'ü' => 'ue', 'Ü' => 'UE', 'ẃ' => 'w', 'Ẃ' => 'W', 'ẁ' => 'w', 'Ẁ' => 'W', 'ŵ' => 'w', 'Ŵ' => 'W', 'ẅ' => 'w', 'Ẅ' => 'W', 'ý' => 'y', 'Ý' => 'Y', 'ỳ' => 'y', 'Ỳ' => 'Y', 'ŷ' => 'y', 'Ŷ' => 'Y', 'ÿ' => 'y', 'Ÿ' => 'Y', 'ź' => 'z', 'Ź' => 'Z', 'ž' => 'z', 'Ž' => 'Z', 'ż' => 'z', 'Ż' => 'Z', 'þ' => 'th', 'Þ' => 'Th', 'µ' => 'u', 'а' => 'a', 'А' => 'a', 'б' => 'b', 'Б' => 'b', 'в' => 'v', 'В' => 'v', 'г' => 'g', 'Г' => 'g', 'д' => 'd', 'Д' => 'd', 'е' => 'e', 'Е' => 'E', 'ё' => 'e', 'Ё' => 'E', 'ж' => 'zh', 'Ж' => 'zh', 'з' => 'z', 'З' => 'z', 'и' => 'i', 'И' => 'i', 'й' => 'j', 'Й' => 'j', 'к' => 'k', 'К' => 'k', 'л' => 'l', 'Л' => 'l', 'м' => 'm', 'М' => 'm', 'н' => 'n', 'Н' => 'n', 'о' => 'o', 'О' => 'o', 'п' => 'p', 'П' => 'p', 'р' => 'r', 'Р' => 'r', 'с' => 's', 'С' => 's', 'т' => 't', 'Т' => 't', 'у' => 'u', 'У' => 'u', 'ф' => 'f', 'Ф' => 'f', 'х' => 'h', 'Х' => 'h', 'ц' => 'c', 'Ц' => 'c', 'ч' => 'ch', 'Ч' => 'ch', 'ш' => 'sh', 'Ш' => 'sh', 'щ' => 'sch', 'Щ' => 'sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'y', 'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'e', 'ю' => 'ju', 'Ю' => 'ju', 'я' => 'ja', 'Я' => 'ja');
$fname = $_FILES[$file]['name'];
$fname = str_replace(array_keys($transliterationTable), array_values($transliterationTable), $fname);
$filename = time() . str_shuffle('pcl48');
$original_filename = preg_replace('/[^(\x20|\x61-\x7A)]*/', '', strtolower(str_ireplace($ext, '', $fname))); //preg_replace('/[^(\x48-\x7A)]*/' strips non-utf character. Ref: http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0000
$_FILES[$file]['name'] = strtolower(substr($original_filename, 0, 100)) . '-' . $filename . $ext;
$attach_id = media_handle_upload($file, $post_id);
if (is_wp_error($attach_id)) {
@unlink($_FILES[$file]['tmp_name']);
echo 'error';
die();
} else {
if ($frames > 1) {
update_post_meta($attach_id, 'a_gif', 'yes');
}
}
}
}
update_post_meta($attach_id, 'pinc_unattached', 'yes');
$return = array();
$thumbnail = wp_get_attachment_image_src($attach_id, 'medium');
$return['thumbnail'] = $thumbnail[0];
$return['id'] = $attach_id;
do_action('pinc_after_upload_pin_computer', $attach_id);
echo json_encode($return);
} else if ($_POST['mode'] == 'web') {
$url = esc_url_raw($_POST['pin_upload_web']);
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$image = curl_exec($ch);
curl_close($ch);
} elseif (ini_get('allow_url_fopen')) {
$image = file_get_contents($url, false, $context);
}
if (!$image) {
echo 'error';
die();
}
$filename = time() . str_shuffle('pcl48');
$file_array['tmp_name'] = WP_CONTENT_DIR . "/" . $filename . '.tmp';
$filetmp = file_put_contents($file_array['tmp_name'], $image);
if (!$filetmp) {
@unlink($file_array['tmp_name']);
echo 'error';
die();
}
$imageTypes = array (
1, //IMAGETYPE_GIF
2, //IMAGETYPE_JPEG
3 //IMAGETYPE_PNG
);
$imageinfo = getimagesize($file_array['tmp_name']);
$width = @$imageinfo[0];
$height = @$imageinfo[1];
$type = @$imageinfo[2];
$mime = @$imageinfo['mime'];
if (!in_array ($type, $imageTypes)) {
@unlink($file_array['tmp_name']);
echo 'error';
die();
}
if ($width < $minWidth || $height < $minWidth) {
@unlink($file_array['tmp_name']);
echo 'errorsize';
die();
}
if($mime != 'image/gif' && $mime != 'image/jpeg' && $mime != 'image/png') {
@unlink($file_array['tmp_name']);
echo 'error';
die();
}
switch($type) {
case 1:
$ext = '.gif';
//check if is animated gif
$frame = 0;
if(($fh = @fopen($file_array['tmp_name'], 'rb')) && $error != 'error') {
while(!feof($fh) && $frames < 2) {
$chunk = fread($fh, 1024 * 100); //read 100kb at a time
$frames += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
}
}
fclose($fh);
break;
case 2:
$ext = '.jpg';
break;
case 3:
$ext = '.png';
break;
}
$original_filename = preg_replace('/[^(\x20|\x61-\x7A)]*/', '', strtolower(str_ireplace($ext, '', basename($url)))); //preg_replace('/[^(\x48-\x7A)]*/' strips non-utf character. Ref: http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0000
$file_array['name'] = strtolower(substr($original_filename, 0, 100)) . '-' . $filename . $ext;
$attach_id = media_handle_sideload($file_array, $post_id);
if (is_wp_error($attach_id)) {
@unlink($file_array['tmp_name']);
echo 'error';
die();
} else {
if ($frames > 1) {
update_post_meta($attach_id, 'a_gif', 'yes');
}
}
update_post_meta($attach_id, 'pinc_unattached', 'yes');
$return = array();
$thumbnail = wp_get_attachment_image_src($attach_id, 'medium');
$return['thumbnail'] = $thumbnail[0];
$return['id'] = $attach_id;
do_action('pinc_after_upload_pin_web', $attach_id);
echo json_encode($return);
}
exit;
}
答案 1 :(得分:0)
对于任何人,如果上述答案不适合你,那么这里是一个删除主题添加的动作钩子的替代方法。
function wpso682524_remove_action()
{
//Remove the unwanted previous hook added by your theme.
remove_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
//Now hook your new function to that hook.
add_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin_new',9);
}
add_action('after_setup_theme', 'wpso682524_remove_action');