2017-01-19 131 views
-1

我在我的WordPress function.php文件中有圖片上傳功能,但我的function.php文件使用了ioncube編碼器編碼,所以我無法編輯該功能。函數覆蓋問題wordpress

但我有一個由我的主題作者給出的函數的完整代碼。並且作者告訴我重寫它將起作用的那個函數。

我的函數名稱爲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

你確定'pinc_upload_pin'掛鉤已經添加,當你試圖刪除它,所以你的代碼不會運行之前,他們添加該掛鉤? –

+0

pinc_upload_pin掛鉤在function.php中添加,我正在刪除header.php中的鉤子我正確嗎? – dhavald99

+0

我的頁腳和函數這兩個文件都被編碼,所以 – dhavald99

回答

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; 
} 
+0

是的,我已經嘗試過高優先級,但功能pinc_upload_pin_new()不是調用我已經把我的新功能退出,但它不是調用什麼問題 – dhavald99

+0

控制檯中是否有任何錯誤?而Ajax調用呢? – Yamu

+0

控制檯中沒有任何錯誤。並且我正在打印media_handle_uplod中的$ _files。如果它進入我們的新函數而不是media_handle_uplod不應該調用,因爲我已經將退出放入了我們的新函數中。 – dhavald99

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');