Wp_get_http已弃用。改用wp_http

时间:2016-03-23 作者:Knott

我使用了一个高级插件(用于主题开发),其中包含WordPress导入器插件,我收到了一个弃用的通知:

“wp\\u get\\u http已deprecated. 改用WP\\U Http“

但是WP\\U Http将我发送到this function.

问题是如何使用wp_http_supports 在以下代码中,而不是wp_get_http:

function fetch_remote_file( $url, $post ) {
    // extract the file name and extension from the url
    $file_name = basename( $url );

    // get placeholder file in the upload dir with a unique, sanitized filename
    $upload = wp_upload_bits( $file_name, 0, \'\', $post[\'upload_date\'] );
    if ( $upload[\'error\'] )
        return new WP_Error( \'upload_dir_error\', $upload[\'error\'] );

    // fetch the remote url and write it to the placeholder file
    $headers = wp_get_http( $url, $upload[\'file\'] );

    // request failed
    if ( ! $headers ) {
        @unlink( $upload[\'file\'] );
        return new WP_Error( \'import_file_error\', __(\'Remote server did not respond\', \'wordpress-importer\') );
    }

    // make sure the fetch was successful
    if ( $headers[\'response\'] != \'200\' ) {
        @unlink( $upload[\'file\'] );
        return new WP_Error( \'import_file_error\', sprintf( __(\'Remote server returned error response %1$d %2$s\', \'wordpress-importer\'), esc_html($headers[\'response\']), get_status_header_desc($headers[\'response\']) ) );
    }

    $filesize = filesize( $upload[\'file\'] );

    if ( isset( $headers[\'content-length\'] ) && $filesize != $headers[\'content-length\'] ) {
        @unlink( $upload[\'file\'] );
        return new WP_Error( \'import_file_error\', __(\'Remote file is incorrect size\', \'wordpress-importer\') );
    }

    if ( 0 == $filesize ) {
        @unlink( $upload[\'file\'] );
        return new WP_Error( \'import_file_error\', __(\'Zero size file downloaded\', \'wordpress-importer\') );
    }

    $max_size = (int) $this->max_attachment_size();
    if ( ! empty( $max_size ) && $filesize > $max_size ) {
        @unlink( $upload[\'file\'] );
        return new WP_Error( \'import_file_error\', sprintf(__(\'Remote file is too large, limit is %s\', \'wordpress-importer\'), size_format($max_size) ) );
    }

    // keep track of the old and new urls so we can substitute them later
    $this->url_remap[$url] = $upload[\'url\'];
    $this->url_remap[$post[\'guid\']] = $upload[\'url\']; // r13735, really needed?
    // keep track of the destination if the remote url is redirected somewhere else
    if ( isset($headers[\'x-final-location\']) && $headers[\'x-final-location\'] != $url )
        $this->url_remap[$headers[\'x-final-location\']] = $upload[\'url\'];

    return $upload;
}
任何帮助都将不胜感激。谢谢

1 个回复
SO网友:birgire

WP_Http 是一个:

用于管理HTTP传输和发出HTTP请求的核心类。

因此,很可能是以下链接:

https://developer.wordpress.org/reference/classes/wp_http/

而不是:

https://developer.wordpress.org/reference/functions/WP_Http

将您重定向到:

https://developer.wordpress.org/reference/functions/wp_http_supports/

还有像这样的包装wp_remote_get(), wp_remote_post(), wp_safe_remote_get(), wp_safe_remote_post(), wp_remote_retrieve_response_code(), download_url(), ... 等等,这使得它更易于使用。

检查,例如HTTP API Codex page

不确定这里的用例是什么,但也有media_handle_sideload(), wp_handle_sideload(), ... 这可能有用。

相关推荐

WooCommerce模板文件输出<strong><code>标签

我正在努力编辑woocommerce模板文件。特别是,我正在尝试创建一个自定义购物车页面,作为指导,我复制了购物车。php模板文件到我的模板文件夹。然而,我写的任何html都包含在<strong><code></code></strong> 标签。我真的不明白为什么,有人有想法吗?下面是一个示例:<?php defined( \'ABSPATH\' ) || exit; do_action( \'woocommerce_before