Hey everyone! I need help creating a JavaScript automation that will grab images stored in Airtable, download them, and then upload them to my WordPress site’s media library. The tricky part is I also want these images to automatically fill in the empty custom image fields I have set up across different post types.
For instance, if I have a user profile for someone like John Smith with an empty avatar field, the script should find the matching image and populate that field automatically. I’m pretty new to programming and learning as I go, so any guidance would be amazing!
Here’s what I’ve got so far for connecting to Airtable:
const airtableClient = new Airtable({ apiKey: 'YOUR_API_KEY' }).base('YOUR_BASE_ID');
const userTable = airtableClient('PROFILES');
const fetchAllRecords = async() => {
const data = await userTable.select({
maxRecords: 100,
view: "Export View"
}).firstPage();
console.log(data);
};
fetchAllRecords();
const fetchSingleRecord = async(recordId) => {
try {
const data = await userTable.find(recordId);
console.log(data);
} catch (error) {
console.error(error);
}
};
fetchSingleRecord();
Any tips on how to connect this to WordPress API and handle the image transfer would be super helpful. Thanks!
You’ll need the WordPress REST API to handle uploads and field population. Here’s what you’re missing:
const uploadToWordPress = async (imageUrl, fileName) => {
const response = await fetch(imageUrl);
const blob = await response.blob();
const formData = new FormData();
formData.append('file', blob, fileName);
const wpResponse = await fetch('https://yoursite.com/wp-json/wp/v2/media', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_WP_TOKEN'
},
body: formData
});
return await wpResponse.json();
};
For custom fields, update posts after upload:
const updatePostField = async (postId, fieldKey, mediaId) => {
await fetch(`https://yoursite.com/wp-json/wp/v2/posts/${postId}`, {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_WP_TOKEN',
'Content-Type': 'application/json'
},
body: JSON.stringify({
meta: {
[fieldKey]: mediaId
}
})
});
};
I’ve done similar migrations before - the key is getting your matching logic right. Search for posts by title or slug to find the correct John Smith profile before updating the avatar field.
Make sure your WordPress site has REST API enabled and proper authentication set up. Application passwords work great for this.
Did something similar last year moving from Airtable to WordPress. Here’s what I learned: First, add delays between API calls or you’ll hit rate limits on both sides. Found this out the hard way. For matching records, don’t search individually - it’s way too slow. Pull all your Airtable data first, then grab WordPress posts and build a lookup table. Match by name or unique IDs, whatever’s most reliable. Watch your image formats and sizes. WordPress is picky and you’ll probably need to validate or convert before uploading. I built in error handling for failed uploads and logged everything so I could track what worked. Custom fields are tricky - depends if you’re using ACF or native WordPress fields. ACF uses different meta key formats (usually with underscores). Definitely test with a small batch first. Trust me, debugging bulk operations when they break is a nightmare.