Skip to content

Instantly share code, notes, and snippets.

@xnau
Created August 18, 2023 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnau/25c8cb69498928bbdf4d6658bb6d0ccd to your computer and use it in GitHub Desktop.
Save xnau/25c8cb69498928bbdf4d6658bb6d0ccd to your computer and use it in GitHub Desktop.
Shows how to set a custom filename for an uploaded image
<?php
/**
* Plugin Name: PDB Custom Image Filename
* Description: names an uploaded image file according to other values in the record
* Version: 1.0
*/
// attach the handler function to the filter
add_action( 'pdb-file_upload_filename', 'pdb_set_custom_filename', 10, 3 );
/**
* sets the custom filename
*
* @filter pdb-file_upload_filename
* @param string $filename the sanitized filename (without extension)
* @param PDb_Form_Field_Def $field_definition the field definition parameters
* @param int|bool $record_id the record id or bool false if the ID hasn't been determined yet (as in a signup form)
* @return string filename without its extension
*/
function pdb_set_custom_filename( $filename, $field_definition, $record_id )
{
// first check to see that we're going to modify the correct image upload field
// the field name of the image we want to modify is "profile_picture"
if ( $field_definition->name() === 'profile_picture' ){
// get the current record data
// this assumes the image upload is taking place after the record has been
// saved, in other words, not during a signup submission
$record = Participants_Db::get_participant($record_id);
// make the filename
$filename = strtolower( $record['first_name'] . '-' . $record['last_name'] );
}
return $filename;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment