Change Column Data Types – Bash Script
This simple script reads a CSV file and outputs that to a PHP function for ACF Custom Datatable to assign a data column type for the fields.
At the moment it only allows for varchar.
1. We’re reading the input file line by line.
2. We’re splitting each line into three variables: datatable, field, and value.
3. We’re creating a function for each line.
4. We’re writing the function to the output file.
#!/bin/bash
input=$PWD/acf-cdt-colunm-data-type-input.csv
echo "/**
* Column Data Types.
*" >> output.txt
echo "" >> output.txt
while IFS="," read -r datatable field value; do
domain=$url
echo "add_filter( 'acfcdt/set_column_data_type/"$datatable"."$field"', '"$field"_set_column_data_type' );
function "$field"_set_column_data_type( $type ) {
return 'varchar("$value")';
}
" >> output.txt
done < <(tail -n +2 $input)
Example CSV File
datatable, field, value
as_button_group,as_button_group,5
as_card,as_snippet,250
as_card,as_condensed,250
Output Example
1. We’re adding a filter to the acfcdt/set_column_data_type/as_button_group.as_button_group
hook.
2. We’re returning the data type varchar(5)
for the as_button_group
field type.
/**
* Colunm Data Types.
*/
add_filter( 'acfcdt/set_column_data_type/as_button_group.as_button_group', 'as_button_group_set_column_data_type' );
function as_button_group_set_column_data_type( ) {
return 'varchar(5)';
}