-
-
Save mkormendy/650268efc2a870d9cf9642306744d09b to your computer and use it in GitHub Desktop.
Used to workaround WP All Import's inability to support curl Auth properly
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. | |
Image examples: https://d.pr/hCfNek and https://d.pr/MnerNb. | |
1. [custom_file_download("ftp://username:[email protected]/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"); | |
/* Optional: Set headers... | |
* $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