-
-
Save trey8611/6fbf6d36b5b86068d86253ccf934eb55 to your computer and use it in GitHub Desktop.
<?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); | |
} |
Where to call the function with file location? I had tried to place this inside my theme functions.php but did not worked
- [custom_file_download("ftp://username:[email protected]/full/path/to/file.csv", "csv")]
- [custom_file_download("http://example.com/full/path/to/file.csv", "csv")]
I have put the php in my theme's function.php
I paste [custom_file_download("ftp://username:[email protected]/full/path/to/file.csv", "csv")] inside the "Download from URL" option within All Import Pro.
I get an error message "There has been a critical error on your website.". Turning on debugging doesn't offer any clues.
I've tried all kinds of variations, but nothing works.
Here's the actual text I paste in, (with the username and password missing)
[custom_file_download("ftp://username:[email protected]/properties.xml", "xml")]
I've tried leaving "xml" as "csv"
Removing the square brackets
Removing the second ftp.
Loads of other stuff..
I've updated the gist, there could be errors if there's a space after the comma in the function call. It should be like this:
[custom_file_download("ftp://username:[email protected]/full/path/to/file.csv","csv")]
Also, we only provide support here: http://www.wpallimport.com/support/.
I have removed space after the comma in the function call but still not working. It is just showing "error:" letter but not showing what was the error
I tried this and still get: <RETS ReplyCode="20513" ReplyText="Miscellaneous Error: Missing required User-Agent request header. See the 'Required Client Request Header Fields' section in the RETS specification."/>
The final URL that it generated was a little off on the shared hosting account I was using, but besides that this gist worked wonderfully to comply with Airtable's new API requirements.
Thanks for sharing. I see that it also now appears on WP All Import's website: https://www.wpallimport.com/documentation/code-snippets/#workaround-for-importing-from-ftp for anyone who that might help.
Dont work if you add this code to functions in settings wp all import. Only functions.php theme