Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active September 21, 2024 21:01
Show Gist options
  • Save cliffordp/fc0fa5686d6a127246224f1565b60128 to your computer and use it in GitHub Desktop.
Save cliffordp/fc0fa5686d6a127246224f1565b60128 to your computer and use it in GitHub Desktop.
WP All Import - set uploads folder to the Import ID
<?php
/**
* WP All Import: Upload media to a directory named after the Import ID.
*
* @link https://gist.github.com/cliffordp/fc0fa5686d6a127246224f1565b60128 This snippet.
* @link https://github.com/soflyy/wp-all-import-action-reference/blob/master/all-import/wp_all_import_images_uploads_dir.php
*
* @see PMXI_Import_Record::process()
* @see wp_upload_dir()
*
* @param array $uploads
* @param array $articleData
* @param array $current_xml_node
* @param $import_id
*
* @return array
*/
function cliff_wpai_set_upload_folder_to_import_id( $uploads, $articleData, $current_xml_node, $import_id ) {
// Ensure not empty or unexpected value, with fallback to zero.
$import_id = absint( $import_id );
$uploads['path'] = $uploads['basedir'] . '/' . $import_id;
$uploads['url'] = $uploads['baseurl'] . '/' . $import_id;
if ( ! file_exists( $uploads['path'] ) ) {
mkdir( $uploads['path'], 0755, true );
}
return $uploads;
}
add_filter( 'wp_all_import_images_uploads_dir', 'cliff_wpai_set_upload_folder_to_import_id', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment