-
-
Save Programmer095/966ea1edb7f222ce65779d40a8ba54f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
################### READ ME ################################# | |
You'll pass the URL to your feed/file to this function inside the "Download from URL" option when creating an import. Examples: | |
1. [custom_file_download("ftp://username:password@hostname.com/full/path/to/file.csv", "csv")] | |
2. [custom_file_download("http://example.com/full/path/to/file.csv", "csv")] | |
You must add the code for the function inside your themes functions.php file, or in a plugin like Code Snippets. | |
This code is provided in the hope that it helps, but without any support. | |
############################################################### | |
*/ | |
// Programmatically download and return import file via URL. | |
function custom_file_download($url, $type = 'xml'){ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); | |
$headers = array(); | |
$headers[] = "Accept-Language: de"; | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$result = curl_exec($ch); | |
if (curl_errno($ch)) { | |
exit('Error:' . curl_error($ch)); | |
} | |
curl_close ($ch); | |
$uploads = wp_upload_dir(); | |
$filename = $uploads['basedir'] . '/' . strtok(basename($url), "?") . '.' . $type; | |
if (file_exists($filename)){ | |
@unlink($filename); | |
} | |
file_put_contents($filename, $result); | |
return str_replace($uploads['basedir'], $uploads['baseurl'], $filename); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment