Skip to content

Instantly share code, notes, and snippets.

@trey8611
Last active March 12, 2024 20:52
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save trey8611/6fbf6d36b5b86068d86253ccf934eb55 to your computer and use it in GitHub Desktop.
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: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");
/* 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);
}
@Just-ars
Copy link

Dont work if you add this code to functions in settings wp all import. Only functions.php theme

@masumbillah21
Copy link

Where to call the function with file location? I had tried to place this inside my theme functions.php but did not worked

  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")]

@jackmalago
Copy link

I have put the php in my theme's function.php
I paste [custom_file_download("ftp://username:password@hostname.com/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:password@ftp.expertagent.co.uk/properties.xml", "xml")]

I've tried leaving "xml" as "csv"
Removing the square brackets
Removing the second ftp.
Loads of other stuff..

@trey8611
Copy link
Author

trey8611 commented Mar 9, 2020

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:password@hostname.com/full/path/to/file.csv","csv")]

Also, we only provide support here: http://www.wpallimport.com/support/.

@jackmalago
Copy link

jackmalago commented Mar 10, 2020 via email

@masumbillah21
Copy link

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

@Becca800
Copy link

Becca800 commented Oct 8, 2020

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."/>

@dugweb
Copy link

dugweb commented Mar 12, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment